day 4 part 2 (in Rust)

main
Andy Teijelo 2023-12-04 16:17:27 -05:00
parent 7d234e0fc2
commit 9e92b66cbe
1 changed files with 20 additions and 1 deletions

View File

@ -1,9 +1,15 @@
use anyhow::Result;
use std::{collections::HashSet, io::stdin};
struct Row {
cards: usize,
matches: usize,
}
fn main() -> Result<()> {
let mut sum = 0;
let mut rows = vec![];
for line in stdin().lines().map_while(Result::ok) {
let p: Vec<_> = line.split(':').collect();
let nums: Vec<_> = p[1].split('|').collect();
@ -22,8 +28,21 @@ fn main() -> Result<()> {
let both = winning.intersection(&mine);
let c = both.count();
sum += if c > 0 { 2i32.pow(c as u32 - 1) } else { 0 };
rows.push(Row {
cards: 1,
matches: c,
});
}
for i in 0..rows.len() {
// let row = &rows[i];
let (front, back) = rows.split_at_mut(i + 1);
let row = front.last().unwrap();
for r in back.iter_mut().take(row.matches) {
r.cards += row.cards;
}
sum += row.cards;
}
println!("{sum}");
Ok(())
}