generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.rs
86 lines (67 loc) · 1.98 KB
/
08.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
advent_of_code::solution!(8);
use advent_of_code::majcn::grid::*;
use advent_of_code::maneatingape::grid::*;
use advent_of_code::maneatingape::hash::*;
struct Block {}
impl Block {
const EMPTY: u8 = b'.';
}
fn parse_data(input: &str) -> Grid<u8> {
Grid::parse(input)
}
fn part_x(grid: Grid<u8>, unlimited: bool) -> u32 {
let mut data = FastMap::new();
grid.points()
.filter(|&p| grid[p] != Block::EMPTY)
.for_each(|p| data.entry(grid[p]).or_insert(vec![]).push(p));
let mut antinodes = grid.same_size_with(false);
for v in data.into_values() {
for &p1 in &v {
for &p2 in &v {
if p1 == p2 {
continue;
}
let diff = p2 - p1;
if unlimited {
let mut new_point = p2;
while grid.contains(new_point) {
antinodes[new_point] = true;
new_point += diff;
}
} else {
let new_point = p2 + diff;
if grid.contains(new_point) {
antinodes[new_point] = true;
}
}
}
}
}
antinodes.bytes.into_iter().filter(|&x| x).count() as u32
}
pub fn part_one(input: &str) -> Option<u32> {
let data = parse_data(input);
let result = part_x(data, false);
Some(result)
}
pub fn part_two(input: &str) -> Option<u32> {
let data = parse_data(input);
let result = part_x(data, true);
Some(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let input = advent_of_code::template::read_file("examples", DAY);
let result = part_one(&input);
assert_eq!(result, Some(14));
}
#[test]
fn test_part_two() {
let input = advent_of_code::template::read_file("examples", DAY);
let result = part_two(&input);
assert_eq!(result, Some(34));
}
}