1
0
Fork 0

day 24 part 1

main
Andrew Coleman 2024-03-01 08:34:34 -05:00
parent 8d1554b542
commit d7d1cdc939
2 changed files with 90 additions and 4 deletions

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

View File

@ -1,10 +1,91 @@
advent_of_code::solution!(24);
pub fn part_one(input: &str) -> Option<u32> {
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<u32> {
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<Hail> {
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::<Vec<_>>()
}
#[cfg(not(test))]
const TARGET: (f64, f64) = (200000000000000., 400000000000000.);
#[cfg(test)]
const TARGET: (f64, f64) = (7., 27.);
pub fn part_one(input: &str) -> Option<usize> {
let stones = parse(input);
let c = stones
.iter()
.combinations(2)
.filter(|st| st[0].intersect(&st[1]))
.count();
Some(c)
}
fn elim(matrix: Vec<f64>) {}
pub fn part_two(input: &str) -> Option<usize> {
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::<Vec<_>>();
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::<Vec<_>>();
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]