mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement 2023 day 6 part 1
This commit is contained in:
@@ -1,7 +1,59 @@
|
|||||||
pub fn part1(_input: &[u8]) -> anyhow::Result<String> {
|
use nom::bytes::complete::tag;
|
||||||
anyhow::bail!("Not implemented")
|
use nom::character::complete::newline;
|
||||||
|
use nom::character::complete::space1;
|
||||||
|
use nom::multi::many1;
|
||||||
|
use nom::sequence::delimited;
|
||||||
|
use nom::sequence::pair;
|
||||||
|
use nom::sequence::preceded;
|
||||||
|
use nom::IResult;
|
||||||
|
|
||||||
|
use crate::common::parse_input;
|
||||||
|
|
||||||
|
fn parse_race(i: &[u8]) -> IResult<&[u8], (Vec<u64>, Vec<u64>)> {
|
||||||
|
use nom::character::complete::u64;
|
||||||
|
|
||||||
|
pair(
|
||||||
|
delimited(tag("Time:"), many1(preceded(space1, u64)), newline),
|
||||||
|
delimited(tag("Distance:"), many1(preceded(space1, u64)), newline),
|
||||||
|
)(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ways(time: u64, distance: u64) -> u64 {
|
||||||
|
let make_it = (1..=time / 2)
|
||||||
|
.filter(|&v| v * (time - v) > distance)
|
||||||
|
.count() as u64;
|
||||||
|
|
||||||
|
if time % 2 == 0 {
|
||||||
|
2 * make_it - 1
|
||||||
|
} else {
|
||||||
|
2 * make_it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part1(input: &[u8]) -> anyhow::Result<String> {
|
||||||
|
let (time, distance) = parse_input(input, parse_race)?;
|
||||||
|
|
||||||
|
let total: u64 = time
|
||||||
|
.iter()
|
||||||
|
.zip(&distance)
|
||||||
|
.map(|(&time, &distance)| ways(time, distance))
|
||||||
|
.product();
|
||||||
|
|
||||||
|
Ok(total.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(_input: &[u8]) -> anyhow::Result<String> {
|
pub fn part2(_input: &[u8]) -> anyhow::Result<String> {
|
||||||
anyhow::bail!("Not implemented")
|
anyhow::bail!("Not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const SAMPLE: &[u8] = include_bytes!("samples/06.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part1() {
|
||||||
|
assert_eq!(part1(SAMPLE).unwrap(), "288");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
2
2023/src/samples/06.txt
Normal file
2
2023/src/samples/06.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
Time: 7 15 30
|
||||||
|
Distance: 9 40 200
|
||||||
Reference in New Issue
Block a user