diff options
Diffstat (limited to '2023/rs/code/four')
-rw-r--r-- | 2023/rs/code/four/src/bin/maddie.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/2023/rs/code/four/src/bin/maddie.rs b/2023/rs/code/four/src/bin/maddie.rs new file mode 100644 index 0000000..dc630a3 --- /dev/null +++ b/2023/rs/code/four/src/bin/maddie.rs @@ -0,0 +1,34 @@ +use std::fs; + +fn main() { + let puzzle_input: String = fs::read_to_string("src/input.txt").expect("Failed to read file"); + let mut total_points = 0; + let mut total_cards = 0; + let mut card_counts: Vec<i32> = vec![1;209]; + + for (i,line) in puzzle_input.lines().enumerate() { + let mut trim = line.split(":"); + trim.next(); + let mut nums = trim.next().unwrap().split("|"); + let winning_nums = nums.next().unwrap().split(" "); + let player_nums = nums.next().unwrap().split(" "); + let mut winning_num_count = 0; + + for num in player_nums { + for winning_num in winning_nums.clone() { + if num == winning_num && num != "" { + winning_num_count += 1; + } + } + } + if winning_num_count != 0 { + total_points += 1 << (winning_num_count-1); + } + for j in (i+1)..(i+winning_num_count+1) { + card_counts[j] += card_counts[i]; + } + total_cards += card_counts[i]; + } + println!("total points: {total_points}"); + println!("total cards: {total_cards}"); +} |