5 Commits

Author SHA1 Message Date
2d2be463d1 Avoid unnecessary floor 2023-12-07 21:52:37 +01:00
e1c23385c9 Replace binary search with maths 2023-12-07 21:45:20 +01:00
f5ca9af74b Clarify order of operations 2023-12-07 21:08:05 +01:00
de24e8b489 Replace sort with counting sort 2023-12-07 21:03:41 +01:00
64f10ff04d Fixed 2023 day 7 2023-12-07 20:48:22 +01:00
4 changed files with 72 additions and 60 deletions

View File

@@ -9,7 +9,7 @@ use criterion::Criterion;
use aoc_2023::get_implementation;
/// 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>> {
let input_path = format!("inputs/{day:02}.txt");

View File

@@ -52,7 +52,7 @@ pub fn part1(input: &[u8]) -> anyhow::Result<String> {
let winners = (card.have & card.winning).count_ones();
if winners > 0 {
1 << winners - 1
1 << (winners - 1)
} else {
0
}

View File

@@ -47,26 +47,23 @@ fn parse_long_race(i: &[u8]) -> IResult<&[u8], (u64, u64)> {
}
fn ways(time: u64, distance: u64) -> u64 {
let half = time / 2;
let mut min = 1;
let mut max = half;
while min < max {
let mid = min + (max - min) / 2;
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
let a = -1.0;
let b = time as f64;
let c = -(distance as f64);
let d = b * b - 4.0 * a * c;
if d < 0.0 {
0
} 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
}
}
}

View File

@@ -1,3 +1,5 @@
use std::mem;
use nom::bytes::complete::tag;
use nom::bytes::complete::take;
use nom::character::complete::newline;
@@ -9,7 +11,7 @@ use nom::Parser;
use crate::common::parse_input;
#[derive(PartialEq, Eq, PartialOrd, Ord)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
enum Kind {
HighCard,
Pair,
@@ -20,35 +22,42 @@ enum Kind {
FiveOfAKind,
}
fn kind_parser1(cards: &[u8; 5]) -> Kind {
#[inline]
fn kind_parser(cards: &[u8; 5], part2: bool) -> Kind {
let mut counts = [0u8; 15];
for &card in cards {
counts[card as usize] += 1;
}
counts.sort_unstable();
match (counts[14], counts[13]) {
(5, _) => Kind::FiveOfAKind,
(4, _) => Kind::FourOfAKind,
(3, 2) => Kind::FullHouse,
(3, _) => Kind::ThreeOfAKind,
(2, 2) => Kind::TwoPair,
(2, _) => Kind::Pair,
_ => Kind::HighCard,
}
}
let jokers = if part2 {
mem::take(&mut counts[1]) as usize
} else {
0
};
fn kind_parser2(cards: &[u8; 5]) -> Kind {
let mut counts = [0u8; 15];
for &card in cards {
counts[card as usize] += 1;
let mut counts_counts = [0u8; 6];
for count in counts {
counts_counts[count as usize] += 1;
}
let jokers = counts[11];
counts[11] = 0;
let mut first = 0;
let mut second = 0;
counts.sort_unstable();
match (counts[14] + jokers, counts[13]) {
for (count, &occurrences) in counts_counts.iter().enumerate() {
match occurrences {
0 => continue,
1 => {
second = first;
first = count;
}
_ => {
first = count;
second = count;
}
}
}
match (first + jokers, second) {
(5, _) => Kind::FiveOfAKind,
(4, _) => Kind::FourOfAKind,
(3, 2) => Kind::FullHouse,
@@ -65,21 +74,27 @@ struct Hand {
kind: Kind,
}
fn hands_parser<'a>(
kind_parser: impl Fn(&[u8; 5]) -> Kind,
) -> impl Parser<&'a [u8], Vec<Hand>, nom::error::Error<&'a [u8]>> {
fn map_card(c: u8) -> anyhow::Result<u8> {
Ok(match c {
d @ b'2'..=b'9' => d - b'0',
b'T' => 10,
b'J' => 11,
b'Q' => 12,
b'K' => 13,
b'A' => 14,
other => anyhow::bail!("Invalid card {other}"),
})
}
#[inline]
fn map_card(c: u8, part2: bool) -> anyhow::Result<u8> {
Ok(match c {
d @ b'2'..=b'9' => d - b'0',
b'T' => 10,
b'J' => {
if part2 {
1
} else {
11
}
}
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(
pair(
terminated(take(5usize), tag(" ")),
@@ -88,9 +103,9 @@ fn hands_parser<'a>(
move |(hand, bid)| -> anyhow::Result<Hand> {
let mut cards = [0; 5];
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 })
},
@@ -115,14 +130,14 @@ fn parts_common(hands: &mut [Hand]) -> 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)
}
// Too high: 248859461
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)
}