Implement 2023 day 8 part 2

This commit is contained in:
2023-12-08 08:10:43 +01:00
parent 8f6937ae42
commit cfea20bed3
4 changed files with 68 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ aho-corasick = "1.1.2"
anyhow = "1.0.75" anyhow = "1.0.75"
clap = { version = "4.4.8", features = ["derive"] } clap = { version = "4.4.8", features = ["derive"] }
nom = "7.1.3" nom = "7.1.3"
num-integer = "0.1.45"
[dev-dependencies] [dev-dependencies]
criterion = "0.5.1" criterion = "0.5.1"

View File

@@ -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 = 7; const DAYS_IMPLEMENTED: u8 = 25;
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");

View File

@@ -1,12 +1,15 @@
use anyhow::Context;
use nom::bytes::complete::tag; use nom::bytes::complete::tag;
use nom::bytes::complete::take; use nom::bytes::complete::take;
use nom::bytes::complete::take_until; use nom::bytes::complete::take_until;
use nom::combinator::map; use nom::combinator::map;
use nom::multi::fold_many1; use nom::multi::fold_many1;
use nom::sequence::preceded;
use nom::sequence::separated_pair; use nom::sequence::separated_pair;
use nom::sequence::terminated; use nom::sequence::terminated;
use nom::sequence::tuple; use nom::sequence::tuple;
use nom::IResult; use nom::IResult;
use num_integer::Integer;
use crate::common::parse_input; use crate::common::parse_input;
@@ -62,6 +65,25 @@ fn parse_map(i: &[u8]) -> IResult<&[u8], Map<'_>> {
)(i) )(i)
} }
fn parse_starts(i: &[u8]) -> IResult<&[u8], Vec<u16>> {
preceded(
tuple((take_until("\n"), tag("\n\n"))),
fold_many1(
terminated(
map(take(3usize), place_to_index),
tuple((take_until("\n"), tag("\n"))),
),
Vec::new,
|mut starts, place| {
if place % 26 == 0 {
starts.push(place)
}
starts
},
),
)(i)
}
pub fn part1(input: &[u8]) -> anyhow::Result<String> { pub fn part1(input: &[u8]) -> anyhow::Result<String> {
let map = parse_input(input, parse_map)?; let map = parse_input(input, parse_map)?;
let end = place_to_index(b"ZZZ"); let end = place_to_index(b"ZZZ");
@@ -78,8 +100,32 @@ pub fn part1(input: &[u8]) -> anyhow::Result<String> {
anyhow::bail!("Unreachable, loop is infinite"); anyhow::bail!("Unreachable, loop is infinite");
} }
pub fn part2(_input: &[u8]) -> anyhow::Result<String> { // This code is wrong. There is no reason that the start of the cycle is indeed the equal to the
anyhow::bail!("Not implemented") // length of the cycle. But it happens to be the case, so we roll with it. Otherwise you could go
// with the full Chinese remainder theorem and knock yourself out that way.
//
// I didn't wanna.
fn find_cycle(map: &Map<'_>, start: u16) -> usize {
let mut pos = start;
for (count, &step) in map.instructions.iter().cycle().enumerate() {
if pos % 26 == 25 {
return count;
}
pos = map.transition(pos, step);
}
unreachable!("Loop is actually infinite")
}
pub fn part2(input: &[u8]) -> anyhow::Result<String> {
let map = parse_input(input, parse_map)?;
let pos = parse_input(input, parse_starts)?;
pos.iter()
.map(|&p| find_cycle(&map, p))
.reduce(|a, b| a.lcm(&b))
.map(|s| s.to_string())
.context("No starting points somehow")
} }
#[cfg(test)] #[cfg(test)]
@@ -88,10 +134,18 @@ mod tests {
const SAMPLE: &[u8] = include_bytes!("samples/08.1.txt"); const SAMPLE: &[u8] = include_bytes!("samples/08.1.txt");
const SAMPLE2: &[u8] = include_bytes!("samples/08.2.txt"); const SAMPLE2: &[u8] = include_bytes!("samples/08.2.txt");
// N.B. sample modified because I don't want to change my parser logic to deal with ascii digits
// in addition to capitals. 1 has been replaced with D, 2 has been replaced with E.
const SAMPLE3: &[u8] = include_bytes!("samples/08.3.txt");
#[test] #[test]
fn sample_part1() { fn sample_part1() {
assert_eq!("2", part1(SAMPLE).unwrap()); assert_eq!("2", part1(SAMPLE).unwrap());
assert_eq!("6", part1(SAMPLE2).unwrap()); assert_eq!("6", part1(SAMPLE2).unwrap());
} }
#[test]
fn sample_part2() {
assert_eq!("6", part2(SAMPLE3).unwrap());
}
} }

10
2023/src/samples/08.3.txt Normal file
View File

@@ -0,0 +1,10 @@
LR
DDA = (DDB, XXX)
DDB = (XXX, DDZ)
DDZ = (DDB, XXX)
EEA = (EEB, XXX)
EEB = (EEC, EEC)
EEC = (EEZ, EEZ)
EEZ = (EEB, EEB)
XXX = (XXX, XXX)