-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.rs
145 lines (125 loc) · 4.96 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
use itertools::Itertools;
use num::Zero;
use crate::input_reader::read_lines;
#[derive(Clone, Debug)]
struct Point(f64, f64, f64);
#[derive(Clone, Debug)]
struct Hail {
start: Point,
end: Point,
}
impl Hail {
fn linear_equation_xy(&self) -> (f64, f64, f64) {
let a = self.end.1 - self.start.1;
let b = self.start.0 - self.end.0;
let c = -1.0 * self.start.0 * (self.end.1 - self.start.1) + self.start.1 * (self.end.0 - self.start.0);
(a, b, c)
}
}
fn find_intersection((a1, b1, c1): (f64, f64, f64), (a2, b2, c2): (f64, f64, f64)) -> Option<(f64, f64)> {
if (a2 * b1 - a1 * b2).is_zero() || (a1 * b2 - a2 * b1).is_zero() {
return None;
}
let x0 = (b2 * c1 - b1 * c2) / (a2 * b1 - a1 * b2);
let y0 = (a2 * c1 - a1 * c2) / (a1 * b2 - a2 * b1);
Some((x0, y0))
}
fn calculate_distance_xy((x1, y1): (f64, f64), (x2, y2): (f64, f64)) -> f64 {
((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)).sqrt()
}
fn parse_hails(input: &str) -> Vec<Hail> {
read_lines(input).iter().map(|l| {
let parts = l.split(" @ ").collect::<Vec<_>>();
let initial_positions = parts[0].split(", ")
.map(|v| v.parse::<f64>().unwrap())
.collect::<Vec<_>>();
let speeds = parts[1].split(", ")
.map(|v| v.trim().parse::<f64>().unwrap())
.collect::<Vec<_>>();
Hail {
start: Point(initial_positions[0],
initial_positions[1],
initial_positions[2]),
end: Point(initial_positions[0] + speeds[0],
initial_positions[1] + speeds[1],
initial_positions[2] + speeds[2]),
}
}).collect::<Vec<_>>()
}
fn count_intersecting_hails(hails: Vec<Hail>, test_area: (f64, f64)) -> usize {
let mut count: usize = 0;
for combinations in hails.into_iter().combinations(2) {
let hail = &combinations[0];
let other = &combinations[1];
if are_hails_intersecting(hail, other, test_area) {
count += 1
}
}
count
}
fn are_hails_intersecting(hail: &Hail, other: &Hail, test_area: (f64, f64)) -> bool {
if let Some(intersection) = find_intersection(hail.linear_equation_xy(), other.linear_equation_xy()) {
let d_1_in = calculate_distance_xy(intersection, (hail.start.0, hail.start.1));
let d_1_f = calculate_distance_xy(intersection, (hail.end.0, hail.end.1));
let d_2_in = calculate_distance_xy(intersection, (other.start.0, other.start.1));
let d_2_f = calculate_distance_xy(intersection, (other.end.0, other.end.1));
return intersection.0 > test_area.0 && intersection.0 < test_area.1 &&
intersection.1 > test_area.0 && intersection.1 < test_area.1 &&
d_1_in > d_1_f && d_2_in > d_2_f;
}
return false;
}
#[cfg(test)]
mod tests {
use indoc::indoc;
// use z3::*;
// use z3::ast::{Ast, Int};
use crate::day24::*;
use crate::input_reader::read_input_file;
#[test]
fn it_solves_first_part() {
let input = &read_input_file("input_day24.txt");
assert_eq!(12740, count_intersecting_hails(parse_hails(input), (200000000000000.0, 400000000000000.0)));
// part 2 right answer is 741991571910536
}
#[test]
fn it_counts_intersecting_hails() {
let input = indoc! {"
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"};
assert_eq!(2, count_intersecting_hails(parse_hails(input), (7.0, 27.0)));
// let cfg = Config::new();
// let ctx = Context::new(&cfg);
// let solver = Solver::new(&ctx);
//
// let x0 = Int::new_const(&ctx, "xo");
// let y0 = Int::new_const(&ctx, "y0");
// let z0 = Int::new_const(&ctx, "z0");
// let vx = Int::new_const(&ctx, "vx");
// let vy = Int::new_const(&ctx, "vy");
// let vz = Int::new_const(&ctx, "vz");
//
// let hails = parse_hails(input);
//
// for index in 0..3 {
// let hail = &hails[index];
// let vhx = Int::from_i64(&ctx,hail.end.0 as i64 - hail.start.0 as i64);
// let vhy = Int::from_i64(&ctx,hail.end.1 as i64 - hail.start.1 as i64);
// let vhz = Int::from_i64(&ctx,hail.end.2 as i64 - hail.start.2 as i64);
// let x = Int::from_i64(&ctx, hail.start.0 as i64);
// let y = Int::from_i64(&ctx, hail.start.1 as i64);
// let z = Int::from_i64(&ctx, hail.start.2 as i64);
// let t = Int::fresh_const(&ctx, &format!("t{}", index + 1));
//
// solver.assert(&(&vhx * &t + &x)._eq(&(*&&vx * &t + &x0)));
// solver.assert(&(&vhy * &t + &y)._eq(&(*&&vy * &t + &y0)));
// solver.assert(&(&vhz * &t + &z)._eq(&(*&&vy * &t + &z0)));
// }
//
// solver.check();
// let model = solver.get_model().unwrap();
}
}