heh
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
#![allow(confusable_idents, uncommon_codepoints, mixed_script_confusables)]
#![feature(array_windows, test, slice_as_chunks, array_chunks)]
extern crate test;
mod util;
pub use explicit_cast::prelude::*;
pub use util::prelude::*;

fn 力(手札: &str) -> u8 {
    let mut 品: HashMap<_, u8> = HashMap::new();
    for &文字 in 手札.as_bytes() {
        *品.entry(文字).or_default() += 1;
    }

    if 品.values().any(|v| *v == 5) {
        return 7;
    }

    if 品.values().any(|v| *v == 4) {
        return 6;
    }

    {
        let mut 数字 = 品.values();
        match 数字.by_ref().next().α() {
            2 => match 数字.by_ref().next().α() {
                3 => return 5,
                _ => {}
            },
            3 => match 数字.by_ref().next().α() {
                2 => return 5,
                _ => {}
            },
            _ => {}
        }
    }

    if 品.values().any(|v| *v == 3) {
        return 4;
    }

    {
        let mut 二人一組 = false;
        for &v in 品.values() {
            if v == 2 {
                if !二人一組 {
                    二人一組 = true
                } else {
                    return 3;
                }
            }
        }
    }

    if 品.values().count() == 4 {
        return 2; // one pair
    }

    if 品.values().count() == 5 {
        return 1; // high card
    }

    dang!();
}

#[test]
fn strengths() {
    assert_eq!(力("AAAAA"), 7);
    assert_eq!(力("AAAAQ"), 6);
    assert_eq!(力("AAAQQ"), 5);
    assert_eq!(力("AAA12"), 4);
    assert_eq!(力("AA110"), 3);
    assert_eq!(力("AA140"), 2);
    assert_eq!(力("A124Q"), 1);
}

fn solve(i: &str) -> impl Display {
    fn карта(x: u8) -> u8 {
        match x {
            b'0'..=b'9' => x - b'0',
            b'T' => 10,
            b'J' => 11,
            b'Q' => 12,
            b'K' => 13,
            b'A' => 14,
            _ => unreachable!(),
        }
    }
    i.lines()
        .map(|x| x.μ(' ').mr(|x| x.λ::<u64>()))
        .sorted_by(|(a, _), (b, _)| match 力(a).cmp(&力(b)) {
            x if matches!(x, Less | Greater) => x,
            _ => {
                for (a, b) in izip!(a.bytes().map(карта), b.bytes().map(карта)) {
                    match a.cmp(&b) {
                        x if matches!(x, Less | Greater) => return x,
                        _ => continue,
                    }
                }
                dang!();
            }
        })
        .rml()
        .ι1()
        .πολλαπλασιάζω_και_αθροίζω()
}

fn main() {
    let i = include_str!("inp.txt").trim();
    println!("{}", solve(i));
}

#[bench]
fn bench(b: &mut test::Bencher) {
    let i = boxd(include_str!("inp.txt").trim());
    b.iter(|| solve(i));
}