diff --git a/src/main.rs b/src/main.rs index ea38a9b..082d0de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(()) }