mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 13:50:32 +01:00
Compare commits
3 Commits
1a45edd3ff
...
63c1024d22
| Author | SHA1 | Date | |
|---|---|---|---|
| 63c1024d22 | |||
| b7797c40aa | |||
| f92261c6b9 |
@@ -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 = 4;
|
const DAYS_IMPLEMENTED: u8 = 5;
|
||||||
|
|
||||||
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");
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use nom::multi::many1;
|
|||||||
use nom::sequence::delimited;
|
use nom::sequence::delimited;
|
||||||
use nom::sequence::pair;
|
use nom::sequence::pair;
|
||||||
use nom::sequence::preceded;
|
use nom::sequence::preceded;
|
||||||
use nom::sequence::tuple;
|
|
||||||
use nom::IResult;
|
use nom::IResult;
|
||||||
|
|
||||||
use crate::common::convert_nom_error;
|
use crate::common::convert_nom_error;
|
||||||
|
|||||||
@@ -1,7 +1,153 @@
|
|||||||
pub fn part1(_input: &[u8]) -> anyhow::Result<String> {
|
use anyhow::Context;
|
||||||
anyhow::bail!("Not implemented")
|
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> {
|
fn parse_mapping(i: &[u8]) -> IResult<&[u8], Vec<Mapping>> {
|
||||||
anyhow::bail!("Not implemented")
|
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
33
2023/src/samples/05.txt
Normal 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
|
||||||
Reference in New Issue
Block a user