summaryrefslogtreecommitdiffstats
path: root/2023/rs/code/four/src/bin/maddie.rs
blob: dc630a303924cec5138d1c2f50f059771809ac6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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}");
}