summaryrefslogtreecommitdiff
path: root/2023/day07.rs
blob: 143f7feefdd2e23109617775c1f7e273bc6a89f1 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::cmp::max;
use std::cmp::Ordering;
use std::fs::File;
use std::io::Read;

#[derive(Debug)]
struct Hand {
    cards: Vec<u8>,
    ct: u32,
    bid: u32,
}

fn count_cards(arr: &Vec<u8>) -> Vec<u32> {
    let mut count = [0; 256].to_vec();
    for a in arr {
        count[*a as usize] += 1;
    }
    let mut count = count
        .iter()
        .filter(|x| **x != 0)
        .copied()
        .collect::<Vec<_>>();
    count.sort();
    count.reverse();
    count
}

fn card_type(count: &Vec<u32>) -> u32 {
    // returns card type from card count
    if count[0] == 5 {
        return 6;
    } else if count[0] == 4 {
        return 5;
    } else if count[0] == 3 && count[1] == 2 {
        return 4;
    } else if count[0] == 3 {
        return 3;
    } else if count[0] == 2 && count[1] == 2 {
        return 2;
    } else if count[0] == 2 {
        return 1;
    } else {
        return 0;
    }
}

fn max_type(count: &Vec<u32>, joker: u32) -> u32 {
    if joker == 0 {
        return card_type(count);
    } else {
        let mut mt = 0;
        for i in 0..count.len() {
            let mut c = count.to_vec();
            c[i] += 1;
            mt = max(mt, max_type(&c, joker - 1));
        }
        return mt;
    }
}

fn get_card_type(c: &Vec<u8>) -> u32 {
    let c_noj: Vec<u8> = c.iter().filter(|x| **x != 0).copied().collect();
    let joker = c
        .iter()
        .fold(0, |acc, x| if *x == 0 { acc + 1 } else { acc });
    let mut count = count_cards(&c_noj);
    count.push(0);

    max_type(&count, joker)
}

fn compare_cards(c1: &Vec<u8>, type1: u32, c2: &Vec<u8>, type2: u32) -> Ordering {
    if type1 > type2 {
        return Ordering::Greater;
    } else if type1 < type2 {
        return Ordering::Less;
    } else {
        for (e1, e2) in c1.iter().zip(c2.iter()) {
            if e1 > e2 {
                return Ordering::Greater;
            } else if e1 < e2 {
                return Ordering::Less;
            }
        }
    }
    Ordering::Equal
}

fn string_to_card(s: &str) -> Vec<u8> {
    // let map = [
    //     '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A',
    // ];
    let map = [
        'J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A',
    ];
    let mut out = Vec::new();
    for c in s.chars() {
        out.push(map.iter().position(|x| x == &c).unwrap() as u8);
    }
    out
}

fn main() {
    // let filename = "in/day07.ref";
    let filename = "in/day07.pzl";

    let mut f = File::open(filename).expect("cannot open file");
    let mut content = String::new();
    f.read_to_string(&mut content).expect("cannot read file");
    let lines: Vec<_> = content.trim_end().split('\n').collect();

    let mut hands: Vec<Hand> = Vec::new();
    for line in lines.iter() {
        let (cards, bid) = line.split_once(" ").unwrap();
        let cards = string_to_card(cards);
        hands.push(Hand {
            cards: cards.clone(),
            bid: bid.parse().unwrap(),
            ct: get_card_type(&cards),
        });
    }

    hands.sort_by(|x, y| compare_cards(&x.cards, x.ct, &y.cards, y.ct));

    let mut res1 = 0;

    let mut res2 = 0;
    for (i, h) in hands.iter().enumerate() {
        // println!("{:?} {}", h.cards, h.ct);
        res2 += (i as u32 + 1) * h.bid;
    }

    println!("res1: {}", res1);
    println!("res2: {}", res2);
}