Day 3 part 2... My struggle in the first half made the second half bliss!

This commit is contained in:
Luke Else 2023-12-03 08:39:30 +00:00
parent 0837967a6b
commit 27a646f88f

View File

@ -22,9 +22,21 @@ impl Solution for Day03 {
fn part2(
&self,
_input: &mut Vec<String>,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
Ok(Box::new("Ready"))
// Get data into a byte array
let lines = input.iter().map(|s| s.as_bytes()).collect::<Vec<_>>();
let mut symbols: HashMap<(usize, usize, char), Vec<usize>> = HashMap::new();
// Get the valid numbers from the input
self.map_valid_nums(&lines, &mut symbols)?;
let ans: usize = symbols
.iter()
.filter(|(&(_, _, s), v)| s == '*' && v.len() == 2)
.map(|(_, v)| v[0] * v[1])
.sum();
Ok(Box::new(ans))
}
fn get_day(&self) -> u8 {
@ -124,6 +136,6 @@ mod test {
.unwrap()
.to_string();
assert_eq!(answer, "Ready");
assert_eq!(answer, "467835");
}
}