Day 4 part 2 complete

day_5_part_2_broken
Joey Pollack 2 years ago
parent b4fd2c4dc5
commit a6470fe6bb

@ -8,13 +8,14 @@ struct Card
id: u32, id: u32,
winning_nums: Vec<u32>, winning_nums: Vec<u32>,
card_nums: Vec<u32>, card_nums: Vec<u32>,
copies: u32,
} }
impl Card impl Card
{ {
fn new() -> 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 += &format!(" {:0>2} ", n);
} }
output += "|";
output += &format!(" {:0>2}", self.copies);
write!(f, "{}", output) write!(f, "{}", output)
} }
} }
@ -43,7 +50,7 @@ fn main()
{ {
let data = load_data("data/input"); let data = load_data("data/input");
let cards = parse_input(&data); let mut cards = parse_input(&data);
// for c in cards // for c in cards
// { // {
@ -51,23 +58,43 @@ fn main()
// } // }
let mut total_points = 0; 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); let mut p = 0;
println!("Card {} points: {}", c.id, points); 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_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 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 points = 0;
let mut wins = 0;
for num in &card.card_nums for num in &card.card_nums
{ {
if card.winning_nums.contains(num) if card.winning_nums.contains(num)
{ {
wins += 1;
if points == 0 if points == 0
{ {
points = 1; points = 1;
@ -79,7 +106,7 @@ fn get_points_on_card(card: &Card) -> u32
} }
} }
points (points, wins)
} }
fn parse_input(input: &str) -> Vec<Card> fn parse_input(input: &str) -> Vec<Card>

Loading…
Cancel
Save