day-01 part-2

This commit is contained in:
Kristofers Solo 2024-12-01 17:38:42 +02:00
parent daf34520d6
commit 3c899e7782

View File

@ -1,8 +1,28 @@
use color_eyre::Result; use color_eyre::Result;
pub fn process(input: &str) -> Result<usize> { pub fn process(input: &str) -> Result<usize> {
todo!("day xx - part 2"); let sum = {
Ok(0) let (firsts, lasts): (Vec<_>, Vec<_>) = input
.lines()
.filter(|line| !line.is_empty())
.map(|line| {
let nums = line
.split_whitespace()
.map(|num| num.parse::<usize>().unwrap())
.collect::<Vec<_>>();
(*nums.first().unwrap(), *nums.last().unwrap())
})
.unzip();
firsts
.iter()
.map(|x| {
let count = lasts.iter().filter(|&y| y == x).count();
x * count
})
.sum()
};
Ok(sum)
} }
#[cfg(test)] #[cfg(test)]
@ -11,9 +31,16 @@ mod tests {
#[test] #[test]
fn test_process() -> Result<()> { fn test_process() -> Result<()> {
let input = ""; let input = "
todo!("haven't built test yet"); 3 4
assert_eq!(0, process(input)?); 4 3
2 5
1 3
3 9
3 3
";
let result = 31;
assert_eq!(process(input)?, result);
Ok(()) Ok(())
} }
} }