-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
189 lines (168 loc) · 4.66 KB
/
mod.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
189
use std::{cmp::Ordering, str::FromStr};
use anyhow::{Context, Result};
use aoc_runner_derive::{aoc, aoc_generator};
#[allow(unused)]
use itertools::Itertools;
use crate::utils::point::Point;
type Output = i64;
type Input = (Vec<Robot>, Point);
const X_LIM: isize = 101;
const Y_LIM: isize = 103;
#[derive(Clone, Copy, Debug)]
pub struct Robot {
position: Point,
speed: Point,
}
impl FromStr for Robot {
type Err = anyhow::Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let re = regex!(r"p=(?<px>\d+),(?<py>\d+) v=(?<vx>-?\d+),(?<vy>-?\d+)");
let captured = re.captures(s).context("Regex not capturing")?;
Ok(Self {
position: Point::from((
captured
.name("px")
.context("Group not captured")?
.as_str()
.parse()
.context("Not a number")?,
captured
.name("py")
.context("Group not captured")?
.as_str()
.parse()
.context("Not a number")?,
)),
speed: Point::from((
captured
.name("vx")
.context("Group not captured")?
.as_str()
.parse()
.context("Not a number")?,
captured
.name("vy")
.context("Group not captured")?
.as_str()
.parse()
.context("Not a number")?,
)),
})
}
}
#[aoc_generator(day14)]
pub fn input_generator(input: &str) -> Result<Input> {
Ok((
input
.lines()
.map(Robot::from_str)
.collect::<Result<Vec<Robot>>>()?,
Point::from((X_LIM, Y_LIM)),
))
}
impl Robot {
pub fn drive(&self, time: isize, limits: Point) -> Self {
Self {
position: self.position.wrapping_add(self.speed * time, limits),
speed: self.speed,
}
}
pub fn get_quadrant(&self, limits: Point) -> Option<usize> {
let half_point_x = limits['x'] / 2;
let half_point_y = limits['y'] / 2;
match (
self.position['x'].cmp(&half_point_x),
self.position['y'].cmp(&half_point_y),
) {
(Ordering::Less, Ordering::Less) => Some(0),
(Ordering::Less, Ordering::Greater) => Some(1),
(Ordering::Greater, Ordering::Less) => Some(2),
(Ordering::Greater, Ordering::Greater) => Some(3),
_ => None,
}
}
}
#[aoc(day14, part1)]
pub fn solve_part1(input: &Input) -> Output {
let (map, limits) = input;
let new_map = map
.iter()
.map(|robot| robot.drive(100, *limits))
.collect::<Vec<Robot>>();
let mut quadrants = [0; 4];
for robot in new_map.iter() {
if let Some(quadrant) = robot.get_quadrant(*limits) {
quadrants[quadrant] += 1;
}
}
quadrants.iter().product()
}
fn print_robots(robots: &[Robot], limits: Point) {
for y in 0..limits['y'] {
for x in 0..limits['x'] {
if robots
.iter()
.map(|r| r.position)
.contains(&Point::from((x, y)))
{
print!("x");
} else {
print!(".");
}
}
println!();
}
}
#[aoc(day14, part2)]
pub fn solve_part2(input: &Input) -> Output {
let (map, limits) = input;
let mut map = map.clone();
let robots = map.len();
let mut seconds = 0;
loop {
map = map
.iter()
.map(|robot| robot.drive(1, *limits))
.collect::<Vec<Robot>>();
seconds += 1;
if map.iter().map(|robot| robot.position).unique().count() == robots {
print_robots(&map, *limits);
break;
}
}
seconds
}
pub fn part1(input: &str) -> impl std::fmt::Display {
solve_part1(&input_generator(input).unwrap())
}
pub fn part2(input: &str) -> impl std::fmt::Display {
solve_part2(&input_generator(input).unwrap())
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> &'static str {
"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 samples_part1() {
let (map, _) = input_generator(&sample()).unwrap();
let limits = Point::from((11, 7));
assert_eq!(12, solve_part1(&(map, limits)));
}
#[test]
fn samples_part2() {
assert!(true);
}
}