summaryrefslogtreecommitdiff
path: root/07/src/part-2.rs
blob: e1f6cedeccbdc17bb7ca776461426aa1e9c38731 (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
mod stuff;
mod stuff_joker;

use crate::stuff::{ascii_to_u64, read_helper};
use crate::stuff_joker::{Card, Hand};


fn main() {
    let mut stdin = std::io::stdin().lock();

    let mut buf: Vec<u8> = Vec::new();
    let mut hands: Vec<(Hand, u64)> = Vec::new();

    loop {
        let num_bytes = read_helper(&mut stdin, &mut buf, b' ').expect("IO error");
        if num_bytes == 0 { break; }
        let cards: [Card; 5] = std::array::from_fn(|i| Card::try_from(buf[i]).expect("Malformed input"));
        let hand = Hand::new(cards);

        let num_bytes = read_helper(&mut stdin, &mut buf, b'\n').expect("IO error");
        if num_bytes == 0 { panic!("Malformed input"); }
        let bid: u64 = ascii_to_u64(& buf);

        hands.push((hand, bid));
    }

    hands.sort_unstable_by(|(h1, _), (h2, _)| h1.cmp(h2));

    let result: u64 = hands.into_iter()
        .zip(1_u64..)
        .map(|((_, bid), rank)| rank*bid)
        .sum();

    println!("{}", result);
}