From 9e92b66cbe23502d2c22010a8df3b2c3c9c522ac Mon Sep 17 00:00:00 2001 From: Andy Teijelo Date: Mon, 4 Dec 2023 16:17:27 -0500 Subject: [PATCH] day 4 part 2 (in Rust) --- src/main.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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(()) }