-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_10.rs
134 lines (122 loc) · 3.85 KB
/
day_10.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
//! https://adventofcode.com/2018/day/10
//! https://adventofcode.com/2018/day/10/input
use std::{fs::read_to_string, time::Instant};
use itertools::Itertools;
use regex::Regex;
struct Light {
x: isize,
y: isize,
horizontal: isize,
vertical: isize,
}
impl From<&str> for Light {
fn from(string: &str) -> Self {
let pattern = Regex::new(r"-?\d+").unwrap();
let mut numbers = pattern.find_iter(string);
Self {
x: numbers.next().unwrap().as_str().parse().unwrap(),
y: numbers.next().unwrap().as_str().parse().unwrap(),
horizontal: numbers.next().unwrap().as_str().parse().unwrap(),
vertical: numbers.next().unwrap().as_str().parse().unwrap(),
}
}
}
fn parse(input: &str) -> Vec<Light> {
input.lines().map(Light::from).collect()
}
fn is_aligned(lights: &[Light]) -> bool {
(lights.iter().map(|light| light.y).max().unwrap()
- lights.iter().map(|light| light.y).min().unwrap())
<= 10
}
fn move_lights(lights: &mut Vec<Light>) -> (usize, String) {
let mut steps = 0;
while !is_aligned(lights) {
lights.iter_mut().for_each(|light| {
light.x += light.horizontal;
light.y += light.vertical;
});
steps += 1;
}
let (min_x, min_y, max_x, max_y) = lights.iter().fold(
(isize::MAX, isize::MAX, isize::MIN, isize::MIN),
|(min_x, min_y, max_x, max_y), light| {
(
min_x.min(light.x),
min_y.min(light.y),
max_x.max(light.x),
max_y.max(light.y),
)
},
);
let mut result = vec![vec![' '; (max_x - min_x + 1) as usize]; (max_y - min_y + 1) as usize];
for light in lights {
result[(light.y - min_y) as usize][(light.x - min_x) as usize] = '#';
}
(
steps,
result
.iter()
.map(|line| line.iter().collect::<String>())
.join("\n"),
)
}
pub mod part1 {
use super::{move_lights, parse};
pub fn solve(input: &str) -> String {
let mut lights = parse(input);
move_lights(&mut lights).1
}
}
pub mod part2 {
use super::{move_lights, parse};
pub fn solve(input: &str) -> usize {
let mut lights = parse(input);
move_lights(&mut lights).0
}
}
pub fn main(test: bool) {
let test_input = "position=< 9, 1> velocity=< 0, 2>
position=< 7, 0> velocity=<-1, 0>
position=< 3, -2> velocity=<-1, 1>
position=< 6, 10> velocity=<-2, -1>
position=< 2, -4> velocity=< 2, 2>
position=<-6, 10> velocity=< 2, -2>
position=< 1, 8> velocity=< 1, -1>
position=< 1, 7> velocity=< 1, 0>
position=<-3, 11> velocity=< 1, -2>
position=< 7, 6> velocity=<-1, -1>
position=<-2, 3> velocity=< 1, 0>
position=<-4, 3> velocity=< 2, 0>
position=<10, -3> velocity=<-1, 1>
position=< 5, 11> velocity=< 1, -2>
position=< 4, 7> velocity=< 0, -1>
position=< 8, -2> velocity=< 0, 1>
position=<15, 0> velocity=<-2, 0>
position=< 1, 6> velocity=< 1, 0>
position=< 8, 9> velocity=< 0, -1>
position=< 3, 3> velocity=<-1, 1>
position=< 0, 5> velocity=< 0, -1>
position=<-2, 2> velocity=< 2, 0>
position=< 5, -2> velocity=< 1, 2>
position=< 1, 4> velocity=< 2, 1>
position=<-2, 7> velocity=< 2, -2>
position=< 3, 6> velocity=<-1, -1>
position=< 5, 0> velocity=< 1, 0>
position=<-6, 0> velocity=< 2, 0>
position=< 5, 9> velocity=< 1, -2>
position=<14, 7> velocity=<-2, 0>
position=<-3, 6> velocity=< 2, -1>"
.to_owned();
let puzzle_input = if test {
test_input
} else {
read_to_string("../inputs/2018/day_10_input.txt").unwrap()
};
let start = Instant::now();
println!("{}", part1::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
let start = Instant::now();
println!("{}", part2::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
}