From a6470fe6bb6a775b5aeb17c9b5fc7ada2f45f064 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Thu, 7 Dec 2023 16:05:00 -0500 Subject: [PATCH] Day 4 part 2 complete --- day_4/src/main.rs | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/day_4/src/main.rs b/day_4/src/main.rs index c9c537b..ca22210 100644 --- a/day_4/src/main.rs +++ b/day_4/src/main.rs @@ -8,13 +8,14 @@ struct Card id: u32, winning_nums: Vec, card_nums: Vec, + copies: u32, } impl Card { fn new() -> Card { - Card { id: 0, winning_nums: Vec::new(), card_nums: Vec::new() } + Card { id: 0, winning_nums: Vec::new(), card_nums: Vec::new(), copies: 1 } } } @@ -35,6 +36,12 @@ impl Display for Card output += &format!(" {:0>2} ", n); } + output += "|"; + + + output += &format!(" {:0>2}", self.copies); + + write!(f, "{}", output) } } @@ -43,7 +50,7 @@ fn main() { let data = load_data("data/input"); - let cards = parse_input(&data); + let mut cards = parse_input(&data); // for c in cards // { @@ -51,23 +58,43 @@ fn main() // } let mut total_points = 0; - for c in cards + let mut total_cards = 0; + for idx in 0..cards.len() { - let points = get_points_on_card(&c); - println!("Card {} points: {}", c.id, points); + let mut p = 0; + let (points, wins) = get_points_on_card(&cards[idx]); + for _ in 0..cards[idx].copies + { + for i in 0..wins + { + cards[idx + 1 + i as usize].copies += 1; + } + + p = points; + } + total_points += points; + total_cards += cards[idx].copies; + + + println!("Card {} points: {}, copies: {}", cards[idx].id, p, cards[idx].copies); + } println!("Total points: {}", total_points); + println!("Total cards with copies: {}", total_cards); } -fn get_points_on_card(card: &Card) -> u32 +fn get_points_on_card(card: &Card) -> (u32, u32) { let mut points = 0; + let mut wins = 0; for num in &card.card_nums { if card.winning_nums.contains(num) { + wins += 1; + if points == 0 { points = 1; @@ -79,7 +106,7 @@ fn get_points_on_card(card: &Card) -> u32 } } - points + (points, wins) } fn parse_input(input: &str) -> Vec