Skyler Grey | 59116e7 | 2023-12-05 00:51:57 +0000 | [diff] [blame] | 1 | use std::collections::HashMap; |
| 2 | |
| 3 | fn main() { |
| 4 | let input = include_str!("../input.txt"); |
| 5 | |
| 6 | let mut scores = vec![]; |
| 7 | let mut scratchcards: HashMap<usize, usize> = HashMap::new(); |
| 8 | |
| 9 | let mut index = 0; |
| 10 | |
| 11 | for card in input.split('\n') { |
| 12 | if card == "" { |
| 13 | continue; |
| 14 | } |
| 15 | |
| 16 | index += 1; |
| 17 | |
| 18 | let data = card.split(": ").collect::<Vec<_>>()[1]; |
| 19 | |
| 20 | let [ winner_str, our_str ] = data.split('|').collect::<Vec<_>>()[..] else { panic!("Invalid card") }; |
| 21 | |
| 22 | let mut winners: Vec<u8> = vec![]; |
| 23 | let mut ours: Vec<u8> = vec![]; |
| 24 | |
| 25 | for num in winner_str.split(' ') { |
| 26 | if num.is_empty() { |
| 27 | continue; // there are some double-spaces in the cards |
| 28 | } |
| 29 | |
| 30 | winners.push(num.parse().unwrap()); |
| 31 | } |
| 32 | |
| 33 | println!("- the winning numbers are {:?}", winners); |
| 34 | |
| 35 | for num in our_str.split(' ') { |
| 36 | if num.is_empty() { |
| 37 | continue; // there are some double-spaces in the cards |
| 38 | } |
| 39 | |
| 40 | ours.push(num.parse().unwrap()); |
| 41 | } |
| 42 | |
| 43 | println!("- our numbers are {:?}", ours); |
| 44 | |
| 45 | let mut n = 0; |
| 46 | for num in ours { |
| 47 | if winners.contains(&num) { |
| 48 | println!(" - {} is a winner!", num); |
| 49 | n += 1 |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if n != 0 { |
| 54 | let score = 2_usize.pow(n - 1); |
| 55 | scores.push(score); |
| 56 | println!("Card {} has {} winning numbers, scoring it {} points!", index, n, score); |
| 57 | |
| 58 | let factor = scratchcards.get(&index).unwrap_or(&0) + 1; |
| 59 | println!("- Adding {} cards to each of the next {} numbers", factor, n); |
| 60 | for i in index+1..index + 1 + TryInto::<usize>::try_into(n).unwrap() { |
| 61 | let old_copies = scratchcards.get(&i).unwrap_or(&0); |
| 62 | println!(" - Adding {} cards to the {} that were already in card {}", factor, old_copies, i); |
| 63 | scratchcards.insert(i, old_copies + factor); |
| 64 | } |
| 65 | } else { |
| 66 | println!("This card didn't win at all, better luck next time..."); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | println!("The total score was {}", scores.iter().sum::<usize>()); |
| 71 | |
| 72 | let clones: usize = scratchcards.into_values().sum(); |
| 73 | println!("You have {} scratchcard clones, plus the original {}, which leaves you with a total of {}", clones, index, clones + index); |
| 74 | } |