-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.rs
153 lines (128 loc) · 4.35 KB
/
day14.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
use crate::solutions::Solution;
use crate::utils::moving_point::MovingPoint;
use crate::utils::point::Point;
use crate::utils::surface_range::SurfaceRange;
use itertools::Itertools;
use std::collections::HashSet;
pub struct Day14 {
surface: SurfaceRange,
}
impl Solution for Day14 {
fn part_one(&self, input: &str) -> String {
let mut robots = self.parse(input);
robots = self.move_all(robots, 100);
let start_x = self.surface.x().start();
let end_x = self.surface.x().end();
let middle_x = self.surface.x().end() / 2;
let start_y = self.surface.y().start();
let end_y = self.surface.y().end();
let middle_y = self.surface.y().end() / 2;
let surface1 = SurfaceRange::from_points(start_x, middle_x - 1, start_y, middle_y - 1);
let surface2 = SurfaceRange::from_points(middle_x + 1, end_x, start_y, middle_y - 1);
let surface3 = SurfaceRange::from_points(start_x, middle_x - 1, middle_y + 1, end_y);
let surface4 = SurfaceRange::from_points(middle_x + 1, end_x, middle_y + 1, end_y);
[surface1, surface2, surface3, surface4]
.iter()
.map(|surface| {
robots
.iter()
.filter(|robot| surface.contains(robot.position()))
.count()
})
.product::<usize>()
.to_string()
}
fn part_two(&self, input: &str) -> String {
let mut robots = self.parse(input);
let mut second = 0;
loop {
second += 1;
robots = self.move_all(robots, 1);
let points = robots.iter().map(|robot| robot.position()).collect_vec();
let points: HashSet<Point> = HashSet::from_iter(points);
// when every robot is on unique position
if points.len() == robots.len() {
return second.to_string();
}
}
}
}
impl Day14 {
fn parse(&self, input: &str) -> Vec<MovingPoint> {
input
.lines()
.map(|s| {
s.split_whitespace()
.collect_tuple()
.map(|(p, v)| {
let position: Point = p.trim_start_matches("p=").parse().unwrap();
let velocity: Point = v.trim_start_matches("v=").parse().unwrap();
MovingPoint::from((position, velocity))
})
.unwrap()
})
.collect()
}
fn move_all(&self, robots: Vec<MovingPoint>, times: isize) -> Vec<MovingPoint> {
robots
.into_iter()
.map(|r| {
let new = r.position() + (r.velocity() * times);
let x = new.x.rem_euclid(self.surface.x().len());
let y = new.y.rem_euclid(self.surface.y().len());
r.with_position(Point::new(x, y))
})
.collect()
}
}
impl Default for Day14 {
fn default() -> Self {
Self {
surface: SurfaceRange::from_points(0, 101 - 1, 0, 103 - 1),
}
}
}
#[cfg(test)]
mod tests {
use crate::solutions::year2024::day14::Day14;
use crate::solutions::Solution;
use crate::utils::moving_point::MovingPoint;
use crate::utils::point::Point;
use crate::utils::surface_range::SurfaceRange;
const EXAMPLE: &str = r#"p=0,4 v=3,-3
p=6,3 v=-1,-3
p=10,3 v=-1,2
p=2,0 v=2,-1
p=0,0 v=1,3
p=3,0 v=-2,-2
p=7,6 v=-1,-3
p=3,0 v=-1,-2
p=9,3 v=2,3
p=7,3 v=-1,2
p=2,4 v=2,-3
p=9,5 v=-3,-3"#;
#[test]
fn part_one_example_test() {
assert_eq!("12", solution().part_one(EXAMPLE));
}
#[test]
fn move_robot_example() {
let robot = MovingPoint::from((Point::new(2, 4), Point::new(2, -3)));
let robot = move_robot(robot);
assert_eq!(Point::new(4, 1), robot.position());
let robot = move_robot(robot);
assert_eq!(Point::new(6, 5), robot.position());
let robot = move_robot(robot);
assert_eq!(Point::new(8, 2), robot.position());
let robot = move_robot(robot);
assert_eq!(Point::new(10, 6), robot.position());
}
fn move_robot(robot: MovingPoint) -> MovingPoint {
solution().move_all(vec![robot], 1).pop().unwrap()
}
fn solution() -> Day14 {
Day14 {
surface: SurfaceRange::from_points(0, 11 - 1, 0, 7 - 1),
}
}
}