-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.rs
188 lines (157 loc) · 4.98 KB
/
day24.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use z3::ast::{Ast, Int};
use z3::{Config, Context, Solver};
type Point = (i64, i64);
// const TEST_SIZE: Point = (7, 27);
const INPUT_SIZE: Point = (200_000_000_000_000, 400_000_000_000_000);
#[derive(Debug)]
struct Vec3<T> {
x: T,
y: T,
z: T,
}
#[derive(Debug)]
struct Line {
a: f64,
b: f64,
c: f64,
}
impl Line {
fn determinant(&self, other: &Line) -> f64 {
self.a * other.b - self.b * other.a
}
}
impl From<Vec<i64>> for Vec3<i64> {
fn from(values: Vec<i64>) -> Self {
if values.len() != 3 {
panic!("Invalid input format.");
}
let [x, y, z] = {
let mut iter = values.into_iter();
[iter.next().unwrap(), iter.next().unwrap(), iter.next().unwrap()]
};
Vec3 { x, y, z }
}
}
#[derive(Debug)]
pub struct Hailstone<T> {
pos: Vec3<T>,
vel: Vec3<T>,
}
impl Hailstone<f64> {
// Converts from parametric form to standard form
fn to_std(&self) -> Line {
Line { a: self.vel.y, b: -self.vel.x, c: (self.vel.y * self.pos.x - self.vel.x * self.pos.y) }
}
}
impl Hailstone<i64> {
fn recast(&self) -> Hailstone<f64> {
Hailstone {
pos: Vec3 { x: self.pos.x as f64, y: self.pos.y as f64, z: self.pos.z as f64 },
vel: Vec3 { x: self.vel.x as f64, y: self.vel.y as f64, z: self.vel.z as f64 },
}
}
}
fn int_in_box(a: &Hailstone<i64>, b: &Hailstone<i64>, bounds: &Point) -> Option<(f64, f64)> {
let (ha, hb) = (a.recast(), b.recast());
let (la, lb) = (ha.to_std(), hb.to_std());
let det = la.determinant(&lb);
if det == 0.0 {
return None;
}
let x = (la.c * lb.b - lb.c * la.b) / det;
let y = (lb.c * la.a - la.c * lb.a) / det;
if ((x - ha.pos.x)/ha.vel.x > 0.0 ) && ((x - hb.pos.x)/hb.vel.x > 0.0 )
&& (bounds.0 as f64..=bounds.1 as f64).contains(&x)
&& (bounds.0 as f64..=bounds.1 as f64).contains(&y) {
return Some((x, y));
}
None
}
fn solve(hs: &Vec<Hailstone<i64>>) -> i64 {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let (px, py, pz, vx, vy, vz) = (
Int::new_const(&ctx, "px"),
Int::new_const(&ctx, "py"),
Int::new_const(&ctx, "pz"),
Int::new_const(&ctx, "vx"),
Int::new_const(&ctx, "vy"),
Int::new_const(&ctx, "vz"),
);
for hailstone in hs {
let (pxn, pyn, pzn, vxn, vyn, vzn) = (
Int::from_i64(&ctx, hailstone.pos.x),
Int::from_i64(&ctx, hailstone.pos.y),
Int::from_i64(&ctx, hailstone.pos.z),
Int::from_i64(&ctx, hailstone.vel.x),
Int::from_i64(&ctx, hailstone.vel.y),
Int::from_i64(&ctx, hailstone.vel.z),
);
let tn = Int::fresh_const(&ctx, "t");
solver.assert(&(&pxn + &vxn * &tn)._eq(&(&px + &vx * &tn)));
solver.assert(&(&pyn + &vyn * &tn)._eq(&(&py + &vy * &tn)));
solver.assert(&(&pzn + &vzn * &tn)._eq(&(&pz + &vz * &tn)));
}
solver.check();
let model = solver.get_model().unwrap();
let (x, y, z) = (
model.get_const_interp(&px).unwrap().as_i64().unwrap(),
model.get_const_interp(&py).unwrap().as_i64().unwrap(),
model.get_const_interp(&pz).unwrap().as_i64().unwrap(),
);
x + y + z
}
#[aoc_generator(day24)]
pub fn input_generator(input: &str) -> Vec<Hailstone<i64>> {
input
.lines()
.map(|line| {
let (position, velocity) = line.trim().split_once(" @ ").unwrap();
let pos: Vec3<i64> = position
.split(", ")
.map(|num| num.trim().parse().expect("Invalid position format."))
.collect::<Vec<_>>()
.try_into()
.unwrap();
let vel: Vec3<i64> = velocity
.split(", ")
.map(|num| num.trim().parse().expect("Invalid velocity format."))
.collect::<Vec<_>>()
.try_into()
.unwrap();
Hailstone { pos, vel }
})
.collect()
}
#[aoc(day24, part1)]
pub fn solve_part1(input: &Vec<Hailstone<i64>>) -> usize {
input
.iter()
.enumerate()
.flat_map(|(i, ha)| input.iter().skip(i + 1).map(move |hb| (ha, hb)))
.filter(|(ha, hb)| int_in_box(ha, hb, &INPUT_SIZE).is_some())
.count()
}
// Part 2 is effectively a ray-tracer, but Z3 SMT solver seems to be a popular solution.
#[aoc(day24, part2)]
pub fn solve_part2(input: &Vec<Hailstone<i64>>) -> i64 {
solve(&input)
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = "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";
#[test]
fn part1_test() {
assert_eq!(solve_part1(&input_generator(TEST)), 2);
}
#[test]
fn part2_test() {
assert_eq!(solve_part2(&input_generator(TEST)), 47);
}
}