Non-functional impl

This commit is contained in:
2023-12-24 15:37:05 +01:00
parent 9cc64a7904
commit bfa7b26c69
2 changed files with 82 additions and 2 deletions

View File

@@ -1,7 +1,82 @@
pub fn part1(_input: &[u8]) -> anyhow::Result<String> {
anyhow::bail!("Not implemented")
use nom::bytes::complete::tag;
use nom::combinator::map;
use nom::multi::many1;
use nom::sequence::terminated;
use nom::sequence::tuple;
use nom::IResult;
use num_integer::Integer;
use crate::common::parse_input;
struct Hail {
position: [i64; 3],
speed: [i64; 3],
}
impl Hail {
fn intersect(&self, other: &Self) -> bool {
// Assumption: speed in no coordinate is 0. This happens to be true.
let multiplier = self.speed[0].lcm(&self.speed[1]);
let mult_x = multiplier / self.speed[0];
let mult_y = multiplier / self.speed[1];
// use the formula for X
false
}
fn parse(i: &[u8]) -> IResult<&[u8], Self> {
use nom::character::complete::i64;
let parse_coordinates = |i| {
map(
tuple((terminated(i64, tag(", ")), terminated(i64, tag(", ")), i64)),
|(x, y, z)| [x, y, z],
)(i)
};
map(
tuple((
terminated(parse_coordinates, tag(" @ ")),
terminated(parse_coordinates, tag("\n")),
)),
|(position, speed)| Self { position, speed },
)(i)
}
}
fn parse_hail(i: &[u8]) -> IResult<&[u8], Vec<Hail>> {
many1(Hail::parse)(i)
}
pub fn part1(input: &[u8]) -> anyhow::Result<String> {
let hail = parse_input(input, parse_hail)?;
let intersections = hail
.iter()
.enumerate()
.flat_map(|(i, a)| {
hail[i + 1..]
.iter()
.map(move |b| (a, b))
.filter(|(a, b)| a.intersect(b))
})
.count();
Ok(intersections.to_string())
}
pub fn part2(_input: &[u8]) -> anyhow::Result<String> {
anyhow::bail!("Not implemented")
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &[u8] = include_bytes!("samples/24.txt");
#[test]
#[ignore = "not completely implemented"]
fn sample_part1() {
assert_eq!("2", part1(SAMPLE).unwrap());
}
}

5
2023/src/samples/24.txt Normal file
View File

@@ -0,0 +1,5 @@
19, 13, 30 @ -2, 1, -2
18, 19, 22 @ -1, -1, -2
20, 25, 34 @ -2, -2, -4
12, 31, 28 @ -1, -2, -1
20, 19, 15 @ 1, -5, -3