From d7d1cdc9392be476caf02619b493b2fd6588ac66 Mon Sep 17 00:00:00 2001 From: Andrew Coleman Date: Fri, 1 Mar 2024 08:34:34 -0500 Subject: [PATCH] day 24 part 1 --- 2023/data/examples/24.txt | 5 +++ 2023/src/bin/24.rs | 89 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/2023/data/examples/24.txt b/2023/data/examples/24.txt index e69de29..d2f1359 100644 --- a/2023/data/examples/24.txt +++ b/2023/data/examples/24.txt @@ -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 diff --git a/2023/src/bin/24.rs b/2023/src/bin/24.rs index c4d88f4..156e5a4 100644 --- a/2023/src/bin/24.rs +++ b/2023/src/bin/24.rs @@ -1,10 +1,91 @@ advent_of_code::solution!(24); -pub fn part_one(input: &str) -> Option { - None +use aoc_parse::{parser, prelude::*}; +use itertools::Itertools; + +struct Hail { + id: usize, + x: f64, + y: f64, + z: f64, + dx: f64, + dy: f64, + dz: f64, } -pub fn part_two(input: &str) -> Option { +impl Hail { + fn intersect(self: &Self, other: &Hail) -> bool { + let bt = ((other.x - self.x) / self.dx - (other.y - self.y) / self.dy) + / (other.dy / self.dy - other.dx / self.dx); + let at = (other.x + bt * other.dx - self.x) / self.dx; + let ix = self.x + self.dx * at; + let iy = self.y + self.dy * at; + bt >= 0.0 + && at >= 0.0 + && ix >= TARGET.0 + && ix <= TARGET.1 + && iy >= TARGET.0 + && iy <= TARGET.1 + } +} + +fn parse(input: &str) -> Vec { + let p = parser!(lines(f64 ", " f64 ", " f64 " @ " f64 ", " f64 ", " f64)); + p.parse(input) + .unwrap() + .iter() + .enumerate() + .map(|(index, h)| Hail { + id: index, + x: h.0, + y: h.1, + z: h.2, + dx: h.3, + dy: h.4, + dz: h.5, + }) + .collect::>() +} + +#[cfg(not(test))] +const TARGET: (f64, f64) = (200000000000000., 400000000000000.); + +#[cfg(test)] +const TARGET: (f64, f64) = (7., 27.); + +pub fn part_one(input: &str) -> Option { + let stones = parse(input); + + let c = stones + .iter() + .combinations(2) + .filter(|st| st[0].intersect(&st[1])) + .count(); + Some(c) +} + +fn elim(matrix: Vec) {} + +pub fn part_two(input: &str) -> Option { + let stones = parse(input); + let xymatrix = stones + .iter() + .map(|s| vec![-s.dy, s.dx, s.y, -s.x, s.y * s.dx - s.x * s.dy]) + .collect::>(); + let lastxy = &xymatrix[xymatrix.len() - 1]; + let xydiff = xymatrix + .iter() + .take(4) + .map(|r| r.iter().zip_eq(lastxy).map(|(a, b)| a - b)); + let zmatrix = stones + .iter() + .map(|s| vec![-s.z, s.dz, s.y, -s.z, s.y * s.dz - s.z * s.dy]) + .collect::>(); + let lastz = &zmatrix[zmatrix.len() - 1]; + let zdiff = zmatrix + .iter() + .take(4) + .map(|r| r.iter().zip_eq(lastz).map(|(a, b)| a - b)); None } @@ -15,7 +96,7 @@ mod tests { #[test] fn test_part_one() { let result = part_one(&advent_of_code::template::read_file("examples", DAY)); - assert_eq!(result, None); + assert_eq!(result, Some(2)); } #[test]