3 Commits

Author SHA1 Message Date
63c1024d22 Brute force day 2023 day 5 part 2 2023-12-05 09:10:16 +01:00
b7797c40aa Implement 2023 day 5 part 1 2023-12-05 09:01:00 +01:00
f92261c6b9 Unused import 2023-12-05 08:20:06 +01:00
4 changed files with 184 additions and 6 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 = 4;
const DAYS_IMPLEMENTED: u8 = 5;
fn read_input(day: u8) -> std::io::Result<Vec<u8>> {
let input_path = format!("inputs/{day:02}.txt");

View File

@@ -8,7 +8,6 @@ use nom::multi::many1;
use nom::sequence::delimited;
use nom::sequence::pair;
use nom::sequence::preceded;
use nom::sequence::tuple;
use nom::IResult;
use crate::common::convert_nom_error;

View File

@@ -1,7 +1,153 @@
pub fn part1(_input: &[u8]) -> anyhow::Result<String> {
anyhow::bail!("Not implemented")
use anyhow::Context;
use nom::bytes::complete::tag;
use nom::character::complete::newline;
use nom::combinator::map;
use nom::multi::many1;
use nom::sequence::delimited;
use nom::sequence::preceded;
use nom::sequence::terminated;
use nom::sequence::tuple;
use nom::IResult;
use crate::common::parse_input;
#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct Mapping {
source_start: u64,
dest_start: u64,
len: u64,
}
pub fn part2(_input: &[u8]) -> anyhow::Result<String> {
anyhow::bail!("Not implemented")
fn parse_mapping(i: &[u8]) -> IResult<&[u8], Vec<Mapping>> {
use nom::character::complete::u64;
let mut mapping = many1(map(
tuple((
terminated(u64, tag(" ")),
terminated(u64, tag(" ")),
terminated(u64, newline),
)),
|(dest_start, source_start, len)| Mapping {
source_start,
dest_start,
len,
},
))(i)?;
// Sort mappings for O(log n) lookup of appropriate mapping later
mapping.1.sort_unstable();
Ok(mapping)
}
struct Almanac {
seeds: Vec<u64>,
// There's a lot of mappings but their names don't matter, they all work the same.
mappings: [Vec<Mapping>; 7],
}
fn parse_almanac(i: &[u8]) -> IResult<&[u8], Almanac> {
let parse_seeds = delimited(
tag("seeds:"),
many1(preceded(tag(" "), nom::character::complete::u64)),
newline,
);
let mapping_parser = |header| preceded(tag(header), parse_mapping);
map(
tuple((
parse_seeds,
mapping_parser("\nseed-to-soil map:\n"),
mapping_parser("\nsoil-to-fertilizer map:\n"),
mapping_parser("\nfertilizer-to-water map:\n"),
mapping_parser("\nwater-to-light map:\n"),
mapping_parser("\nlight-to-temperature map:\n"),
mapping_parser("\ntemperature-to-humidity map:\n"),
mapping_parser("\nhumidity-to-location map:\n"),
)),
|(seeds, soil, fertilizer, water, light, temperature, humidity, location)| Almanac {
seeds,
mappings: [
soil,
fertilizer,
water,
light,
temperature,
humidity,
location,
],
},
)(i)
}
fn follow_mapping(node: u64, mappings: &[Mapping]) -> u64 {
let point = mappings.partition_point(|mapping| mapping.source_start <= node);
if point == 0 {
// There are no mappings that are smaller than the node, so it maps to itself
node
} else {
// `mapping`` is the last mapping that starts smaller or equal to our node, so it is the one
// that might contain it.
let mapping = &mappings[point - 1];
// Check if the node is in range of this mapping
if node - mapping.source_start < mapping.len {
// It is, note the order of operations to avoid underflow
node + mapping.dest_start - mapping.source_start
} else {
// It's not, return itself
node
}
}
}
fn follow_all_mappings(mut node: u64, mappings: &[Vec<Mapping>]) -> u64 {
for mappings in mappings {
node = follow_mapping(node, mappings)
}
node
}
pub fn part1(input: &[u8]) -> anyhow::Result<String> {
let almanac = parse_input(input, parse_almanac)?;
let min = almanac
.seeds
.iter()
.map(|node| follow_all_mappings(*node, &almanac.mappings))
.min()
.context("Unreachable, no seeds but parser ensures seeds")?;
Ok(min.to_string())
}
pub fn part2(input: &[u8]) -> anyhow::Result<String> {
let almanac = parse_input(input, parse_almanac)?;
let min = almanac
.seeds
.chunks_exact(2)
.flat_map(|c| c[0]..c[0] + c[1])
.map(|node| follow_all_mappings(node, &almanac.mappings))
.min()
.context("Unreachable, no seeds but parser ensures seeds")?;
Ok(min.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &[u8] = include_bytes!("samples/05.txt");
#[test]
fn sample_part1() {
assert_eq!(part1(SAMPLE).unwrap(), "35");
}
#[test]
fn sample_part2() {
assert_eq!(part2(SAMPLE).unwrap(), "46");
}
}

33
2023/src/samples/05.txt Normal file
View File

@@ -0,0 +1,33 @@
seeds: 79 14 55 13
seed-to-soil map:
50 98 2
52 50 48
soil-to-fertilizer map:
0 15 37
37 52 2
39 0 15
fertilizer-to-water map:
49 53 8
0 11 42
42 0 7
57 7 4
water-to-light map:
88 18 7
18 25 70
light-to-temperature map:
45 77 23
81 45 19
68 64 13
temperature-to-humidity map:
0 69 1
1 0 69
humidity-to-location map:
60 56 37
56 93 4