day 7 part 1
This commit is contained in:
parent
b2bd814c8a
commit
6b09514019
5 changed files with 1112 additions and 58 deletions
25
Cargo.lock
generated
25
Cargo.lock
generated
|
@ -22,10 +22,17 @@ name = "aoc2023"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"counter",
|
||||
"fancy-regex",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "bit-set"
|
||||
version = "0.5.3"
|
||||
|
@ -41,6 +48,15 @@ version = "0.6.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
|
||||
|
||||
[[package]]
|
||||
name = "counter"
|
||||
version = "0.5.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2d458e66999348f56fd3ffcfbb7f7951542075ca8359687c703de6500c1ddccd"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fancy-regex"
|
||||
version = "0.12.0"
|
||||
|
@ -57,6 +73,15 @@ version = "2.6.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.2"
|
||||
|
|
|
@ -7,5 +7,6 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
anyhow = "1.0.75"
|
||||
counter = "0.5.7"
|
||||
fancy-regex = "0.12.0"
|
||||
regex = "1.10.2"
|
||||
|
|
|
@ -1,2 +1,5 @@
|
|||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
||||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
||||
|
|
135
src/main.rs
135
src/main.rs
|
@ -1,76 +1,103 @@
|
|||
use std::io::stdin;
|
||||
use std::{collections::HashMap, io::stdin};
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use counter::Counter;
|
||||
use regex::Regex;
|
||||
|
||||
/// solve ax² + bx + c = 0
|
||||
fn solve_quadratic(a: f64, b: f64, c: f64) -> (f64, f64) {
|
||||
let r1 = (-b + f64::sqrt(b * b - 4.0 * a * c)) / 2.0 * a;
|
||||
let r2 = (-b - f64::sqrt(b * b - 4.0 * a * c)) / 2.0 * a;
|
||||
(r1, r2)
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
enum HandType {
|
||||
FiveOfAKind,
|
||||
FourOfAKind,
|
||||
FullHouse,
|
||||
ThreeOfAKind,
|
||||
TwoPair,
|
||||
OnePair,
|
||||
HighCard,
|
||||
}
|
||||
|
||||
fn parse_input() -> Result<(Vec<i64>, Vec<i64>)> {
|
||||
let numbers = Regex::new(r"(\d+)")?;
|
||||
let mut times = vec![];
|
||||
let mut distances = vec![];
|
||||
for line in stdin().lines().map_while(Result::ok) {
|
||||
if line.starts_with("Time:") {
|
||||
times = numbers
|
||||
.captures_iter(&line.replace(' ', ""))
|
||||
.map(|c| c.get(1).unwrap().as_str().parse())
|
||||
.map_while(Result::ok)
|
||||
.collect();
|
||||
} else if line.starts_with("Distance:") {
|
||||
distances = numbers
|
||||
.captures_iter(&line.replace(' ', ""))
|
||||
.map(|c| c.get(1).unwrap().as_str().parse())
|
||||
.map_while(Result::ok)
|
||||
.collect();
|
||||
} else {
|
||||
bail!("unexpected line: {}", line);
|
||||
fn hand_type(hand: &str) -> HandType {
|
||||
let counter: Counter<char> = hand.chars().collect();
|
||||
if counter.keys().len() == 1 {
|
||||
return HandType::FiveOfAKind;
|
||||
}
|
||||
if counter.keys().len() == 2 {
|
||||
if counter.k_most_common_ordered(1)[0].1 == 4 {
|
||||
return HandType::FourOfAKind;
|
||||
}
|
||||
if counter.k_most_common_ordered(1)[0].1 == 3 {
|
||||
return HandType::FullHouse;
|
||||
}
|
||||
}
|
||||
Ok((times, distances))
|
||||
if counter.keys().len() == 3 {
|
||||
if counter.k_most_common_ordered(1)[0].1 == 3 {
|
||||
return HandType::ThreeOfAKind;
|
||||
}
|
||||
if counter.k_most_common_ordered(1)[0].1 == 2 {
|
||||
return HandType::TwoPair;
|
||||
}
|
||||
}
|
||||
if counter.keys().len() == 4 {
|
||||
return HandType::OnePair;
|
||||
}
|
||||
HandType::HighCard
|
||||
}
|
||||
|
||||
type CmpKey = (u8, u8, u8, u8, u8, u8);
|
||||
|
||||
fn hand_cmp_key(hand: &str) -> CmpKey {
|
||||
let hand_strength = HashMap::from([
|
||||
(HandType::FiveOfAKind, 7u8),
|
||||
(HandType::FourOfAKind, 6u8),
|
||||
(HandType::FullHouse, 5u8),
|
||||
(HandType::ThreeOfAKind, 4u8),
|
||||
(HandType::TwoPair, 3u8),
|
||||
(HandType::OnePair, 2u8),
|
||||
(HandType::HighCard, 1u8),
|
||||
]);
|
||||
let card_strength: HashMap<char, u8> = "AKQJT98765432"
|
||||
.chars()
|
||||
.enumerate()
|
||||
.map(|(i, c)| (c, (14 - i) as u8))
|
||||
.collect();
|
||||
|
||||
let mut r = vec![*hand_strength.get(&hand_type(hand)).unwrap()];
|
||||
r.extend(hand.chars().map(|c| card_strength.get(&c).unwrap()));
|
||||
|
||||
(r[0], r[1], r[2], r[3], r[4], r[5])
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let (times, distances) = parse_input()?;
|
||||
let mut bids = vec![];
|
||||
for line in stdin().lines().map_while(Result::ok) {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
let hand = parts[0];
|
||||
let bid: i64 = parts[1].parse().unwrap();
|
||||
println!("{} {:?} {}", hand, hand_type(hand), bid);
|
||||
|
||||
if times.len() != distances.len() {
|
||||
bail!(
|
||||
"times has {} elements, but distances has {} elements",
|
||||
times.len(),
|
||||
distances.len()
|
||||
);
|
||||
bids.push((hand.to_owned(), bid));
|
||||
}
|
||||
|
||||
let mut prod = 1;
|
||||
for i in 0..times.len() {
|
||||
let t = times[i];
|
||||
let d = distances[i];
|
||||
|
||||
// the equation to solve is x * (t - x) - d = 0
|
||||
// = t*x - x*x - d = 0
|
||||
// = -1x² + tx - d = 0
|
||||
let (r1, r2) = solve_quadratic(-1.0, t as f64, -d as f64);
|
||||
let r1 = (r1 + 1.0).floor() as i64;
|
||||
let r2 = (r2 - 1.0).ceil() as i64;
|
||||
|
||||
let ways = (r2 - r1) + 1;
|
||||
|
||||
println!("{t} {d}");
|
||||
println!("ways: {ways}");
|
||||
prod *= ways;
|
||||
bids.sort_by_key(|(hand, _)| hand_cmp_key(hand));
|
||||
let mut winnings = 0;
|
||||
for (i, b) in bids.iter().enumerate() {
|
||||
let (_, bid) = b;
|
||||
winnings += (i as i64 + 1) * bid;
|
||||
}
|
||||
|
||||
println!("{prod}");
|
||||
println!("{}", winnings);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test() {}
|
||||
fn test_hand_types() {
|
||||
assert_eq!(hand_type("AAAAA"), HandType::FiveOfAKind);
|
||||
assert_eq!(hand_type("AA8AA"), HandType::FourOfAKind);
|
||||
assert_eq!(hand_type("23332"), HandType::FullHouse);
|
||||
assert_eq!(hand_type("TTT98"), HandType::ThreeOfAKind);
|
||||
assert_eq!(hand_type("23432"), HandType::TwoPair);
|
||||
assert_eq!(hand_type("A23A4"), HandType::OnePair);
|
||||
assert_eq!(hand_type("23456"), HandType::HighCard);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue