mirror of
https://github.com/kristoferssolo/Advent-of-Code.git
synced 2025-10-21 18:00:35 +00:00
day-01 part-2
This commit is contained in:
parent
daf34520d6
commit
3c899e7782
@ -1,8 +1,28 @@
|
||||
use color_eyre::Result;
|
||||
|
||||
pub fn process(input: &str) -> Result<usize> {
|
||||
todo!("day xx - part 2");
|
||||
Ok(0)
|
||||
let sum = {
|
||||
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)]
|
||||
@ -11,9 +31,16 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_process() -> Result<()> {
|
||||
let input = "";
|
||||
todo!("haven't built test yet");
|
||||
assert_eq!(0, process(input)?);
|
||||
let input = "
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
";
|
||||
let result = 31;
|
||||
assert_eq!(process(input)?, result);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user