generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.rs
96 lines (76 loc) · 2.26 KB
/
11.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
advent_of_code::solution!(11);
use std::collections::HashSet;
#[derive(PartialEq, Eq, Hash)]
struct Point {
x: usize,
y: usize,
}
struct Grid {
data: HashSet<Point>,
max_x: usize,
max_y: usize,
}
fn parse_data(input: &str) -> Grid {
let mut max_x = 0;
let mut max_y = 0;
let mut data = HashSet::new();
for (y, line) in input.lines().enumerate() {
for (x, v) in line.as_bytes().iter().enumerate() {
if v == &b'#' {
data.insert(Point { x, y });
max_x = usize::max(max_x, x);
max_y = usize::max(max_y, y);
}
}
}
Grid { data, max_x, max_y }
}
fn part_x<const N: usize>(grid: Grid) -> u64 {
let expand_x = (0..=grid.max_x)
.filter(|&x| !(0..=grid.max_y).any(|y| grid.data.contains(&Point { x, y })))
.collect::<Vec<_>>();
let expand_y = (0..=grid.max_y)
.filter(|&y| !(0..=grid.max_x).any(|x| grid.data.contains(&Point { x, y })))
.collect::<Vec<_>>();
let grid_data_as_vec = grid
.data
.into_iter()
.map(|p| Point {
x: p.x + (N - 1) * expand_x.iter().filter(|ex| &p.x > ex).count(),
y: p.y + (N - 1) * expand_y.iter().filter(|ey| &p.y > ey).count(),
})
.collect::<Vec<_>>();
let mut result = 0;
for i in 0..grid_data_as_vec.len() {
for j in (i + 1)..grid_data_as_vec.len() {
let p1 = &grid_data_as_vec[i];
let p2 = &grid_data_as_vec[j];
result += usize::abs_diff(p1.x, p2.x) + usize::abs_diff(p1.y, p2.y)
}
}
result as u64
}
pub fn part_one(input: &str) -> Option<u64> {
let grid = parse_data(input);
let result = part_x::<2>(grid);
Some(result)
}
pub fn part_two(input: &str) -> Option<u64> {
let grid = parse_data(input);
let result = part_x::<1000000>(grid);
Some(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(374));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(82000210));
}
}