mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement 2023 day 6 part 2
This commit is contained in:
@@ -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 = 5;
|
const DAYS_IMPLEMENTED: u8 = 6;
|
||||||
|
|
||||||
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");
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
use nom::bytes::complete::tag;
|
use nom::bytes::complete::tag;
|
||||||
|
use nom::character::complete::digit1;
|
||||||
use nom::character::complete::newline;
|
use nom::character::complete::newline;
|
||||||
use nom::character::complete::space1;
|
use nom::character::complete::space1;
|
||||||
|
use nom::multi::fold_many1;
|
||||||
use nom::multi::many1;
|
use nom::multi::many1;
|
||||||
use nom::sequence::delimited;
|
use nom::sequence::delimited;
|
||||||
use nom::sequence::pair;
|
use nom::sequence::pair;
|
||||||
@@ -10,12 +12,38 @@ use nom::IResult;
|
|||||||
use crate::common::parse_input;
|
use crate::common::parse_input;
|
||||||
|
|
||||||
fn parse_race(i: &[u8]) -> IResult<&[u8], (Vec<u64>, Vec<u64>)> {
|
fn parse_race(i: &[u8]) -> IResult<&[u8], (Vec<u64>, Vec<u64>)> {
|
||||||
use nom::character::complete::u64;
|
let line = |header| {
|
||||||
|
delimited(
|
||||||
|
tag(header),
|
||||||
|
many1(preceded(space1, nom::character::complete::u64)),
|
||||||
|
newline,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
pair(
|
pair(line("Time:"), line("Distance:"))(i)
|
||||||
delimited(tag("Time:"), many1(preceded(space1, u64)), newline),
|
}
|
||||||
delimited(tag("Distance:"), many1(preceded(space1, u64)), newline),
|
|
||||||
)(i)
|
fn parse_long_race(i: &[u8]) -> IResult<&[u8], (u64, u64)> {
|
||||||
|
let line = |header| {
|
||||||
|
delimited(
|
||||||
|
tag(header),
|
||||||
|
fold_many1(
|
||||||
|
preceded(space1, digit1),
|
||||||
|
|| 0,
|
||||||
|
|mut cur, sequence| {
|
||||||
|
for &c in sequence {
|
||||||
|
cur *= 10;
|
||||||
|
cur += u64::from(c - b'0');
|
||||||
|
}
|
||||||
|
|
||||||
|
cur
|
||||||
|
},
|
||||||
|
),
|
||||||
|
newline,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
pair(line("Time:"), line("Distance:"))(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ways(time: u64, distance: u64) -> u64 {
|
fn ways(time: u64, distance: u64) -> u64 {
|
||||||
@@ -42,8 +70,10 @@ pub fn part1(input: &[u8]) -> anyhow::Result<String> {
|
|||||||
Ok(total.to_string())
|
Ok(total.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(_input: &[u8]) -> anyhow::Result<String> {
|
pub fn part2(input: &[u8]) -> anyhow::Result<String> {
|
||||||
anyhow::bail!("Not implemented")
|
let (time, distance) = parse_input(input, parse_long_race)?;
|
||||||
|
|
||||||
|
Ok(ways(time, distance).to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -56,4 +86,9 @@ mod tests {
|
|||||||
fn sample_part1() {
|
fn sample_part1() {
|
||||||
assert_eq!(part1(SAMPLE).unwrap(), "288");
|
assert_eq!(part1(SAMPLE).unwrap(), "288");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part2() {
|
||||||
|
assert_eq!(part2(SAMPLE).unwrap(), "71503");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user