generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.rs
162 lines (138 loc) · 4.99 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
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
use std::collections::{HashMap, HashSet};
advent_of_code::solution!(8);
pub fn part_one(input: &str) -> Option<u32> {
let grid = input
.lines()
.map(|l| l.chars().collect::<Vec<_>>())
.collect::<Vec<_>>();
// let mut antennas: HashMap<char, Vec<(usize, usize)>> = HashMap::new();
// let grid = input.lines().map(|l| l.as_bytes()).collect::<Vec<_>>();
let mut freqs = HashMap::<_, Vec<_>>::new();
for i in 0..grid.len() {
for j in 0..grid[0].len() {
if grid[i][j] == '.' {
continue;
}
freqs.entry(grid[i][j]).or_default().push((i, j));
}
}
let grid = input
.lines()
.map(|l| l.chars().collect::<Vec<_>>())
.collect::<Vec<_>>();
let mut positions: HashMap<char, Vec<(i32, i32)>> = HashMap::new();
for (y, row) in grid.iter().enumerate() {
for (x, c) in row.iter().enumerate() {
if *c != '.' {
positions.entry(*c).or_default().push((x as i32, y as i32));
}
}
}
let mut antinodes_part1 = HashSet::new();
let mut antinodes_part2 = HashSet::new();
for antennas in positions.values() {
for i in 0..antennas.len() {
for j in i + 1..antennas.len() {
let dx = antennas[j].0 - antennas[i].0;
let dy = antennas[j].1 - antennas[i].1;
let mut n = 1;
loop {
let nxi = antennas[i].0 + dx * n;
let nyi = antennas[i].1 + dy * n;
let nxj = antennas[j].0 - dx * n;
let nyj = antennas[j].1 - dy * n;
let mut ok = 0;
if nxi >= 0 && nyi >= 0 && nxi < grid[0].len() as i32 && nyi < grid.len() as i32
{
if n == 2 {
antinodes_part1.insert((nxi, nyi));
}
antinodes_part2.insert((nxi, nyi));
ok += 1;
}
if nxj >= 0 && nyj >= 0 && nxj < grid[0].len() as i32 && nyj < grid.len() as i32
{
if n == 2 {
antinodes_part1.insert((nxj, nyj));
}
antinodes_part2.insert((nxj, nyj));
ok += 1;
}
if ok == 0 {
break;
}
n += 1;
}
}
}
}
Some(antinodes_part1.len() as u32)
}
pub fn part_two(input: &str) -> Option<u32> {
let grid = input
.lines()
.map(|l| l.chars().collect::<Vec<_>>())
.collect::<Vec<_>>();
let mut positions: HashMap<char, Vec<(i32, i32)>> = HashMap::new();
for (y, row) in grid.iter().enumerate() {
for (x, c) in row.iter().enumerate() {
if *c != '.' {
positions.entry(*c).or_default().push((x as i32, y as i32));
}
}
}
let mut antinodes_part1 = HashSet::new();
let mut antinodes_part2 = HashSet::new();
for antennas in positions.values() {
for i in 0..antennas.len() {
for j in i + 1..antennas.len() {
let dx = antennas[j].0 - antennas[i].0;
let dy = antennas[j].1 - antennas[i].1;
let mut n = 1;
loop {
let nxi = antennas[i].0 + dx * n;
let nyi = antennas[i].1 + dy * n;
let nxj = antennas[j].0 - dx * n;
let nyj = antennas[j].1 - dy * n;
let mut ok = 0;
if nxi >= 0 && nyi >= 0 && nxi < grid[0].len() as i32 && nyi < grid.len() as i32
{
if n == 2 {
antinodes_part1.insert((nxi, nyi));
}
antinodes_part2.insert((nxi, nyi));
ok += 1;
}
if nxj >= 0 && nyj >= 0 && nxj < grid[0].len() as i32 && nyj < grid.len() as i32
{
if n == 2 {
antinodes_part1.insert((nxj, nyj));
}
antinodes_part2.insert((nxj, nyj));
ok += 1;
}
if ok == 0 {
break;
}
n += 1;
}
}
}
}
// println!("{}", antinodes_part2.len());
Some(antinodes_part2.len() as u32)
}
#[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(14));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(34));
}
}