heh
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs106
1 files changed, 95 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs
index 5fa28ae..8bfa5dd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,23 +1,107 @@
-#![allow(confusable_idents, uncommon_codepoints)]
+#![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::*;
-const S: char = ' ';
+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 {
- let mut lines = i.lines();
- let time = lines.Δ().μ1(':').ͷ().fold(0, |acc, n| acc * 10 + n as u64);
- let distance = lines.Δ().μ1(':').ͷ().fold(0, |acc, n| acc * 10 + n as u64);
- let mut works = 0;
- for held in 0..time {
- let gone = held * (time - held);
- if gone > distance {
- works += 1;
+ 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!(),
}
}
- works
+ 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() {