diff --git a/four/output-two b/four/output-two new file mode 100644 index 0000000..ad9b3db --- /dev/null +++ b/four/output-two @@ -0,0 +1 @@ +8736438 diff --git a/four/src/two.rs b/four/src/two.rs index 9db4926..f937369 100644 --- a/four/src/two.rs +++ b/four/src/two.rs @@ -3,4 +3,47 @@ use std::io::{self, Read}; fn main() { let mut buf = String::new(); io::stdin().read_to_string(&mut buf).unwrap(); + + let wins_by_card: Vec<_> = buf + .lines() + .map(|l| &l[5..]) + .map(|l| [&l[5..34], &l[37..]]) + .map(|[winners, numbers]| { + ( + winners + .split(' ') + .filter(|str| str != &"") + .map(str::trim) + .flat_map(|num_str| num_str.parse::()) + .collect::>(), + numbers + .split(' ') + .filter(|str| str != &"") + .map(str::trim) + .flat_map(|num_str| num_str.parse::()) + .collect::>(), + ) + }) + .map(|(winners, numbers)| { + numbers + .iter() + .filter(|number| winners.contains(number)) + .count() + }) + .collect(); + + let mut copies_by_card = vec![1; wins_by_card.len()]; + + wins_by_card.iter().enumerate().for_each(|(idx, wins)| { + let copies_of_winner = copies_by_card[idx]; + copies_by_card + .iter_mut() + .skip(idx + 1) + .take(*wins) + .for_each(|copies| *copies += copies_of_winner) + }); + + let total: usize = copies_by_card.iter().sum(); + + println!("{total}"); }