| use std::collections::HashMap; |
| |
| fn main() { |
| let input = include_str!("../input.txt"); |
| |
| let mut scores = vec![]; |
| let mut scratchcards: HashMap<usize, usize> = HashMap::new(); |
| |
| let mut index = 0; |
| |
| for card in input.split('\n') { |
| if card == "" { |
| continue; |
| } |
| |
| index += 1; |
| |
| let data = card.split(": ").collect::<Vec<_>>()[1]; |
| |
| let [ winner_str, our_str ] = data.split('|').collect::<Vec<_>>()[..] else { panic!("Invalid card") }; |
| |
| let mut winners: Vec<u8> = vec![]; |
| let mut ours: Vec<u8> = vec![]; |
| |
| for num in winner_str.split(' ') { |
| if num.is_empty() { |
| continue; // there are some double-spaces in the cards |
| } |
| |
| winners.push(num.parse().unwrap()); |
| } |
| |
| println!("- the winning numbers are {:?}", winners); |
| |
| for num in our_str.split(' ') { |
| if num.is_empty() { |
| continue; // there are some double-spaces in the cards |
| } |
| |
| ours.push(num.parse().unwrap()); |
| } |
| |
| println!("- our numbers are {:?}", ours); |
| |
| let mut n = 0; |
| for num in ours { |
| if winners.contains(&num) { |
| println!(" - {} is a winner!", num); |
| n += 1 |
| } |
| } |
| |
| if n != 0 { |
| let score = 2_usize.pow(n - 1); |
| scores.push(score); |
| println!("Card {} has {} winning numbers, scoring it {} points!", index, n, score); |
| |
| let factor = scratchcards.get(&index).unwrap_or(&0) + 1; |
| println!("- Adding {} cards to each of the next {} numbers", factor, n); |
| for i in index+1..index + 1 + TryInto::<usize>::try_into(n).unwrap() { |
| let old_copies = scratchcards.get(&i).unwrap_or(&0); |
| println!(" - Adding {} cards to the {} that were already in card {}", factor, old_copies, i); |
| scratchcards.insert(i, old_copies + factor); |
| } |
| } else { |
| println!("This card didn't win at all, better luck next time..."); |
| } |
| } |
| |
| println!("The total score was {}", scores.iter().sum::<usize>()); |
| |
| let clones: usize = scratchcards.into_values().sum(); |
| println!("You have {} scratchcard clones, plus the original {}, which leaves you with a total of {}", clones, index, clones + index); |
| } |