advent-of-code/2023/src/bin/day07.rs

122 lines
3.5 KiB
Rust
Raw Normal View History

2023-12-07 11:19:37 +01:00
#![feature(test)]
extern crate test;
2023-12-07 12:49:37 +01:00
2023-12-07 11:19:37 +01:00
use aoc2023::{boilerplate, common::*};
use itertools::Itertools;
const DAY: usize = 7;
type I = usize;
2023-12-07 12:49:37 +01:00
type Hand = [u8; 5];
type Parsed = Vec<(Hand, I)>;
2023-12-07 11:19:37 +01:00
2023-12-07 13:36:19 +01:00
const CARDS: [u8; 14] = [b' ', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'T', b'J', b'Q', b'K', b'A'];
2023-12-07 12:49:37 +01:00
2023-12-07 12:57:07 +01:00
#[derive(Debug, PartialEq)]
2023-12-07 12:49:37 +01:00
enum Quality {
AllEqual = 1 << 30,
Quad = 1 << 29,
FullHouse = 1 << 28,
Triple = 1 << 27,
TwoPair = 1 << 26,
Pair = 1 << 25,
None = 1 << 24,
}
impl Quality {
// How adding 1 joker raises the quality of a hand.
fn upgrade(&self) -> Self {
match self {
Quality::AllEqual | Quality::Quad => Quality::AllEqual,
Quality::Triple | Quality::FullHouse => Quality::Quad,
Quality::TwoPair => Quality::FullHouse,
Quality::Pair => Quality::Triple,
Quality::None => Quality::Pair,
}
}
}
2023-12-07 11:19:37 +01:00
fn parse_input(raw: &str) -> Parsed {
raw.lines()
.map(|l| l.split_once(' ').unwrap())
2023-12-07 12:49:37 +01:00
.map(|(hand, points)| (<[_; 5]>::try_from(hand.as_bytes()).unwrap(), parse_num(points)))
2023-12-07 11:19:37 +01:00
.collect()
}
2023-12-07 12:49:37 +01:00
fn rate_hand(mut hand: Hand) -> Quality {
hand.sort();
2023-12-07 13:36:19 +01:00
let equals = hand
2023-12-07 12:49:37 +01:00
.into_iter()
.map(|c| (c, 1))
.coalesce(|(c1, n1), (c2, n2)| if c1 == c2 { Ok((c1, n1 + n2)) } else { Err(((c1, n1), (c2, n2))) })
.map(|(_, n)| n)
.collect_vec();
2023-12-07 13:36:19 +01:00
match equals.as_slice() {
2023-12-07 12:49:37 +01:00
[_] => Quality::AllEqual,
[4, 1] | [1, 4] => Quality::Quad,
[3, 2] | [2, 3] => Quality::FullHouse,
2023-12-07 13:36:19 +01:00
[3, 1, 1] | [1, 3, 1] | [1, 1, 3] => Quality::Triple,
2023-12-07 12:49:37 +01:00
[_, _, _] => Quality::TwoPair,
[_, _, _, _] => Quality::Pair,
_ => Quality::None,
}
}
2023-12-07 13:36:19 +01:00
fn tiebreak(hand: &Hand) -> usize {
hand.iter().map(|b| CARDS.iter().position(|c| c == b).unwrap_or(0)).fold(0, |acc, n| (acc << 4) + n)
2023-12-07 12:49:37 +01:00
}
2023-12-07 11:19:37 +01:00
fn part1(hands: &Parsed) -> usize {
2023-12-07 13:36:19 +01:00
let mut rated_hands = hands.iter().cloned().map(|(hand, points)| (tiebreak(&hand) + rate_hand(hand) as usize, points)).collect_vec();
2023-12-07 11:19:37 +01:00
rated_hands.sort_unstable_by_key(|(v, _)| *v);
rated_hands.into_iter().zip(1..).map(|((_, points), position)| points * position).sum()
}
2023-12-07 12:49:37 +01:00
fn part2(hands: &Parsed) -> usize {
let mut rated_hands = hands
.iter()
.cloned()
.map(|(mut hand, points)| {
// Count how many jokers there are and insert nonsense data for them so they can’t produce any pairs.
2023-12-07 13:36:19 +01:00
let jokers = hand.iter_mut().filter(|c| c == &&b'J').zip(0..).map(|(c, i)| *c = i).count();
let tiebreak = tiebreak(&hand);
let rating = (0..jokers).fold(rate_hand(hand), |rating, _| rating.upgrade());
2023-12-07 12:49:37 +01:00
(rating as usize + tiebreak, points)
})
.collect_vec();
rated_hands.sort_unstable_by_key(|(v, _)| *v);
rated_hands.into_iter().zip(1..).map(|((_, points), position)| points * position).sum()
2023-12-07 11:19:37 +01:00
}
boilerplate! {
TEST_INPUT == "\
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483",
tests: {
part1: { TEST_INPUT => 6440 },
2023-12-07 12:49:37 +01:00
part2: { TEST_INPUT => 5905 },
2023-12-07 11:19:37 +01:00
},
bench1 == 248812215,
2023-12-07 12:49:37 +01:00
bench2 == 250057090,
2023-12-07 11:19:37 +01:00
bench_parse: Vec::len => 1000,
}
2023-12-07 12:57:07 +01:00
#[cfg(test)]
mod bench {
use test::black_box;
use super::*;
#[bench]
fn bench_tiebreak(b: &mut test::Bencher) {
2023-12-07 13:36:19 +01:00
b.iter(|| assert_eq!(tiebreak(black_box(&[b'3', b'2', b'T', b'3', b'K'])), 137516))
2023-12-07 12:57:07 +01:00
}
#[bench]
fn bench_rate_hand(b: &mut test::Bencher) {
b.iter(|| assert_eq!(rate_hand(black_box([b'3', b'2', b'T', b'3', b'K'])), Quality::Pair))
}
}