-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_05.rs
208 lines (177 loc) · 6.05 KB
/
day_05.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::Solution;
const DATA: &str = include_str!("data/5");
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
struct Point {
x: i32,
y: i32,
}
#[derive(Debug, PartialEq, Clone)]
struct Line(Point, Point);
impl Point {
fn extend(&self, extend: Vec<(i32, i32)>) -> Vec<Point> {
extend.iter()
.map(|(x, y)| Point { x: self.x + x, y: self.y + y })
.collect()
}
}
impl Line {
fn to_points_vertically(&self) -> Vec<Point> {
let delta = self.1.x - self.0.x;
let vec_x = match delta >= 0 {
true => (0..=delta).collect::<Vec<i32>>(),
false => (delta..=0).collect::<Vec<i32>>(),
};
let vec_y: Vec<i32> = vec![0; delta.abs() as usize + 1];
let vec: Vec<(i32, i32)> = vec_x.iter()
.zip(vec_y.iter())
.map(|(x, y)| (*x, *y))
.collect();
self.0.extend(vec)
}
fn to_points_horizontally(&self) -> Vec<Point> {
let delta = self.1.y - self.0.y;
let vec_y = match delta >= 0 {
true => (0..=delta).collect::<Vec<i32>>(),
false => (delta..=0).collect::<Vec<i32>>(),
};
let vec_x: Vec<i32> = vec![0; delta.abs() as usize + 1];
let vec: Vec<(i32, i32)> = vec_x.iter()
.zip(vec_y.iter())
.map(|(x, y)| (*x, *y))
.collect();
self.0.extend(vec)
}
fn to_points_diagonally(&self) -> Vec<Point> {
let delta_x = self.1.x - self.0.x;
let delta_y = self.1.y - self.0.y;
debug_assert_eq!(delta_y.abs(), delta_x.abs());
let vec_x = match delta_x >= 0 {
true => (0..=delta_x).collect::<Vec<i32>>(),
false => (delta_x..=0).rev().collect::<Vec<i32>>(),
};
let vec_y = match delta_y >= 0 {
true => (0..=delta_y).collect::<Vec<i32>>(),
false => (delta_y..=0).rev().collect::<Vec<i32>>(),
};
let vec: Vec<(i32, i32)> = vec_x.iter()
.zip(vec_y.iter())
.map(|(x, y)| (*x, *y))
.collect();
self.0.extend(vec)
}
fn extend(&self, use_diag: bool) -> Vec<Point> {
return {
if self.0.x != self.1.x && self.0.y == self.1.y {
self.to_points_vertically()
} else if self.0.x == self.1.x && self.0.y != self.1.y {
self.to_points_horizontally()
} else if use_diag {
self.to_points_diagonally()
} else {
Vec::new()
}
};
}
fn from_str(s: &str) -> Line {
let numbers: Vec<i32> = s.split(",").map(|n|
{
n.parse::<i32>().expect(&format!("Failed parsing: {}", n))
})
.collect();
debug_assert_eq!(numbers.len(), 4);
Line(
Point { x: numbers[0], y: numbers[1] },
Point { x: numbers[2], y: numbers[3] },
)
}
}
pub(crate) struct Day {
lines: Vec<Line>,
}
impl Day {
fn parse_data(s: &str) -> Day {
let data = s.replace(" -> ", ",");
let lines: Vec<Line> = data.lines().map(|row| Line::from_str(row)).collect();
Day { lines }
}
fn count_overlapping_points(&self, use_diag: bool) -> usize {
let points: Vec<Point> = self.lines.iter()
.map(|l| l.extend(use_diag))
.flatten()
.collect();
let max_x = points.iter().fold(0, |init, item| i32::max(init, item.x));
let max_y = points.iter().fold(0, |init, item| i32::max(init, item.y));
let mut grid = vec![vec![0; 1 + max_x as usize]; 1 + max_y as usize];
for point in &points {
grid[point.y as usize][point.x as usize] += 1;
}
let count = grid.iter().flatten().filter(|v| **v >= 2).count();
count
}
pub fn new() -> Day { Day::parse_data(DATA) }
}
impl Solution for Day {
fn name(&self) -> &'static str { "Day 5: Hydrothermal Venture" }
fn part_a(&self) -> Option<String> {
let count = self.count_overlapping_points(false);
Some(format!("{}", count))
}
fn part_b(&self) -> Option<String> {
let count = self.count_overlapping_points(true);
Some(format!("{}", count))
}
}
#[cfg(test)]
mod tests {
use super::*;
const TEST_DATA: &str = "0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2";
#[test]
/// Parses a single row of the input
fn test_line_from_str() {
let s = "0,9,5,9";
let line = Line(Point { x: 0, y: 9 }, Point { x: 5, y: 9 });
assert_eq!(Line::from_str(s), line);
}
#[test]
/// Extends a single point
fn test_point_extend() {
let p = Point { x: 0, y: 0 };
// Extending vertically
let vec_x: Vec<(i32, i32)> = vec![(0, 0), (1, 0)];
assert_eq!(p.extend(vec_x), vec![Point { x: 0, y: 0 }, Point { x: 1, y: 0 }]);
// Extending horizontally
let vec_x: Vec<(i32, i32)> = vec![(0, 0), (0, 1)];
assert_eq!(p.extend(vec_x), vec![Point { x: 0, y: 0 }, Point { x: 0, y: 1 }]);
// Extending diagonally
let vec_x: Vec<(i32, i32)> = vec![(0, 0), (1, 1)];
assert_eq!(p.extend(vec_x), vec![Point { x: 0, y: 0 }, Point { x: 1, y: 1 }]);
}
#[test]
fn test_line_to_points() {
let l = Line(Point { x: 0, y: 0 }, Point { x: 1, y: 1 });
// Extending vertically
assert_eq!(l.to_points_vertically(), vec![Point { x: 0, y: 0 }, Point { x: 1, y: 0 }]);
// Extending horizontally
assert_eq!(l.to_points_horizontally(), vec![Point { x: 0, y: 0 }, Point { x: 0, y: 1 }]);
// Extending diagonally
assert_eq!(l.to_points_diagonally(), vec![Point { x: 0, y: 0 }, Point { x: 1, y: 1 }]);
}
#[test]
fn test_count_overlapping_points() {
let day = Day::parse_data(TEST_DATA);
let count = day.count_overlapping_points(false);
assert_eq!(count, 5);
let day = Day::parse_data(TEST_DATA);
let count = day.count_overlapping_points(true);
assert_eq!(count, 12);
}
}