mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 22:00:31 +01:00
Compare commits
5 Commits
cb5af961bc
...
2d2be463d1
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d2be463d1 | |||
| e1c23385c9 | |||
| f5ca9af74b | |||
| de24e8b489 | |||
| 64f10ff04d |
@@ -9,7 +9,7 @@ use criterion::Criterion;
|
|||||||
use aoc_2023::get_implementation;
|
use aoc_2023::get_implementation;
|
||||||
|
|
||||||
/// Number of days we have an implementation to benchmark
|
/// Number of days we have an implementation to benchmark
|
||||||
const DAYS_IMPLEMENTED: u8 = 6;
|
const DAYS_IMPLEMENTED: u8 = 7;
|
||||||
|
|
||||||
fn read_input(day: u8) -> std::io::Result<Vec<u8>> {
|
fn read_input(day: u8) -> std::io::Result<Vec<u8>> {
|
||||||
let input_path = format!("inputs/{day:02}.txt");
|
let input_path = format!("inputs/{day:02}.txt");
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ pub fn part1(input: &[u8]) -> anyhow::Result<String> {
|
|||||||
let winners = (card.have & card.winning).count_ones();
|
let winners = (card.have & card.winning).count_ones();
|
||||||
|
|
||||||
if winners > 0 {
|
if winners > 0 {
|
||||||
1 << winners - 1
|
1 << (winners - 1)
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,26 +47,23 @@ fn parse_long_race(i: &[u8]) -> IResult<&[u8], (u64, u64)> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn ways(time: u64, distance: u64) -> u64 {
|
fn ways(time: u64, distance: u64) -> u64 {
|
||||||
let half = time / 2;
|
let a = -1.0;
|
||||||
let mut min = 1;
|
let b = time as f64;
|
||||||
let mut max = half;
|
let c = -(distance as f64);
|
||||||
|
let d = b * b - 4.0 * a * c;
|
||||||
while min < max {
|
if d < 0.0 {
|
||||||
let mid = min + (max - min) / 2;
|
0
|
||||||
|
|
||||||
if mid * (time - mid) <= distance {
|
|
||||||
min = mid + 1;
|
|
||||||
} else {
|
|
||||||
max = mid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let make_it = half - min + 1;
|
|
||||||
|
|
||||||
if time % 2 == 0 {
|
|
||||||
2 * make_it - 1
|
|
||||||
} else {
|
} else {
|
||||||
2 * make_it
|
// Note: can leave out quite a bit of the quadratic formula because things cancel out nicely
|
||||||
|
let solution = ((b - d.sqrt()) / 2.0 + 1.0) as u64;
|
||||||
|
let half = time / 2;
|
||||||
|
let make_it = half - solution + 1;
|
||||||
|
|
||||||
|
if time % 2 == 0 {
|
||||||
|
2 * make_it - 1
|
||||||
|
} else {
|
||||||
|
2 * make_it
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::mem;
|
||||||
|
|
||||||
use nom::bytes::complete::tag;
|
use nom::bytes::complete::tag;
|
||||||
use nom::bytes::complete::take;
|
use nom::bytes::complete::take;
|
||||||
use nom::character::complete::newline;
|
use nom::character::complete::newline;
|
||||||
@@ -9,7 +11,7 @@ use nom::Parser;
|
|||||||
|
|
||||||
use crate::common::parse_input;
|
use crate::common::parse_input;
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||||
enum Kind {
|
enum Kind {
|
||||||
HighCard,
|
HighCard,
|
||||||
Pair,
|
Pair,
|
||||||
@@ -20,35 +22,42 @@ enum Kind {
|
|||||||
FiveOfAKind,
|
FiveOfAKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn kind_parser1(cards: &[u8; 5]) -> Kind {
|
#[inline]
|
||||||
|
fn kind_parser(cards: &[u8; 5], part2: bool) -> Kind {
|
||||||
let mut counts = [0u8; 15];
|
let mut counts = [0u8; 15];
|
||||||
for &card in cards {
|
for &card in cards {
|
||||||
counts[card as usize] += 1;
|
counts[card as usize] += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
counts.sort_unstable();
|
let jokers = if part2 {
|
||||||
match (counts[14], counts[13]) {
|
mem::take(&mut counts[1]) as usize
|
||||||
(5, _) => Kind::FiveOfAKind,
|
} else {
|
||||||
(4, _) => Kind::FourOfAKind,
|
0
|
||||||
(3, 2) => Kind::FullHouse,
|
};
|
||||||
(3, _) => Kind::ThreeOfAKind,
|
|
||||||
(2, 2) => Kind::TwoPair,
|
|
||||||
(2, _) => Kind::Pair,
|
|
||||||
_ => Kind::HighCard,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn kind_parser2(cards: &[u8; 5]) -> Kind {
|
let mut counts_counts = [0u8; 6];
|
||||||
let mut counts = [0u8; 15];
|
for count in counts {
|
||||||
for &card in cards {
|
counts_counts[count as usize] += 1;
|
||||||
counts[card as usize] += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let jokers = counts[11];
|
let mut first = 0;
|
||||||
counts[11] = 0;
|
let mut second = 0;
|
||||||
|
|
||||||
counts.sort_unstable();
|
for (count, &occurrences) in counts_counts.iter().enumerate() {
|
||||||
match (counts[14] + jokers, counts[13]) {
|
match occurrences {
|
||||||
|
0 => continue,
|
||||||
|
1 => {
|
||||||
|
second = first;
|
||||||
|
first = count;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
first = count;
|
||||||
|
second = count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match (first + jokers, second) {
|
||||||
(5, _) => Kind::FiveOfAKind,
|
(5, _) => Kind::FiveOfAKind,
|
||||||
(4, _) => Kind::FourOfAKind,
|
(4, _) => Kind::FourOfAKind,
|
||||||
(3, 2) => Kind::FullHouse,
|
(3, 2) => Kind::FullHouse,
|
||||||
@@ -65,21 +74,27 @@ struct Hand {
|
|||||||
kind: Kind,
|
kind: Kind,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hands_parser<'a>(
|
#[inline]
|
||||||
kind_parser: impl Fn(&[u8; 5]) -> Kind,
|
fn map_card(c: u8, part2: bool) -> anyhow::Result<u8> {
|
||||||
) -> impl Parser<&'a [u8], Vec<Hand>, nom::error::Error<&'a [u8]>> {
|
Ok(match c {
|
||||||
fn map_card(c: u8) -> anyhow::Result<u8> {
|
d @ b'2'..=b'9' => d - b'0',
|
||||||
Ok(match c {
|
b'T' => 10,
|
||||||
d @ b'2'..=b'9' => d - b'0',
|
b'J' => {
|
||||||
b'T' => 10,
|
if part2 {
|
||||||
b'J' => 11,
|
1
|
||||||
b'Q' => 12,
|
} else {
|
||||||
b'K' => 13,
|
11
|
||||||
b'A' => 14,
|
}
|
||||||
other => anyhow::bail!("Invalid card {other}"),
|
}
|
||||||
})
|
b'Q' => 12,
|
||||||
}
|
b'K' => 13,
|
||||||
|
b'A' => 14,
|
||||||
|
other => anyhow::bail!("Invalid card {other}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn hands_parser<'a>(part2: bool) -> impl Parser<&'a [u8], Vec<Hand>, nom::error::Error<&'a [u8]>> {
|
||||||
many1(map_res(
|
many1(map_res(
|
||||||
pair(
|
pair(
|
||||||
terminated(take(5usize), tag(" ")),
|
terminated(take(5usize), tag(" ")),
|
||||||
@@ -88,9 +103,9 @@ fn hands_parser<'a>(
|
|||||||
move |(hand, bid)| -> anyhow::Result<Hand> {
|
move |(hand, bid)| -> anyhow::Result<Hand> {
|
||||||
let mut cards = [0; 5];
|
let mut cards = [0; 5];
|
||||||
for (t, &s) in cards.iter_mut().zip(hand) {
|
for (t, &s) in cards.iter_mut().zip(hand) {
|
||||||
*t = map_card(s)?
|
*t = map_card(s, part2)?
|
||||||
}
|
}
|
||||||
let kind = kind_parser(&cards);
|
let kind = kind_parser(&cards, part2);
|
||||||
|
|
||||||
Ok(Hand { cards, bid, kind })
|
Ok(Hand { cards, bid, kind })
|
||||||
},
|
},
|
||||||
@@ -115,14 +130,14 @@ fn parts_common(hands: &mut [Hand]) -> anyhow::Result<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn part1(input: &[u8]) -> anyhow::Result<String> {
|
pub fn part1(input: &[u8]) -> anyhow::Result<String> {
|
||||||
let mut hands = parse_input(input, hands_parser(kind_parser1))?;
|
let mut hands = parse_input(input, hands_parser(false))?;
|
||||||
|
|
||||||
parts_common(&mut hands)
|
parts_common(&mut hands)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Too high: 248859461
|
// Too high: 248859461
|
||||||
pub fn part2(input: &[u8]) -> anyhow::Result<String> {
|
pub fn part2(input: &[u8]) -> anyhow::Result<String> {
|
||||||
let mut hands = parse_input(input, hands_parser(kind_parser2))?;
|
let mut hands = parse_input(input, hands_parser(true))?;
|
||||||
|
|
||||||
parts_common(&mut hands)
|
parts_common(&mut hands)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user