-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday08.rs
More file actions
154 lines (135 loc) · 3.8 KB
/
Copy pathday08.rs
File metadata and controls
154 lines (135 loc) · 3.8 KB
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
use std::cmp::max;
use std::collections::{HashMap, HashSet};
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
struct Point {
row: i32,
col: i32,
}
impl Point {
pub fn minus(&self, other: &Point) -> Point {
Point {
row: self.row - other.row,
col: self.col - other.col,
}
}
pub fn plus(&self, other: &Point) -> Point {
Point {
row: self.row + other.row,
col: self.col + other.col,
}
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
struct Grid {
n_rows: i32,
n_cols: i32,
antennas: HashMap<char, Vec<Point>>,
}
impl Grid {
fn contains(&self, point: &Point) -> bool {
0 <= point.col && point.col < self.n_cols && 0 <= point.row && point.row < self.n_rows
}
fn get_antinode(&self, antenna: char, with_harmonics: bool) -> Option<HashSet<Point>> {
let points = self.antennas.get(&antenna)?;
let mut result = HashSet::new();
let steps = if with_harmonics {
max(self.n_cols, self.n_cols)
} else {
1
};
for p in points.iter() {
for q in points.iter() {
if p == q {
continue;
}
let delta = p.minus(q);
let mut candidate = if with_harmonics {
p.clone()
} else {
p.plus(&delta)
};
for _ in 0..steps {
if self.contains(&candidate) {
result.insert(candidate.clone());
} else {
break;
}
candidate = candidate.plus(&delta);
}
}
}
Some(result)
}
pub fn get_antinodes(&self, with_harmonics: bool) -> HashSet<Point> {
let mut result = HashSet::new();
for antenna in self.antennas.keys() {
if let Some(antinode) = self.get_antinode(*antenna, with_harmonics) {
result.extend(antinode);
}
}
result
}
}
fn parse_input(input: &str) -> Grid {
let mut antennas: HashMap<char, Vec<Point>> = HashMap::new();
let mut n_rows = 0;
let mut n_cols: i32 = 0;
for (row, line) in input.lines().enumerate() {
if line.is_empty() {
continue;
}
let signed_row = i32::try_from(row).expect("Too many rows for i32");
n_rows += 1;
n_cols = i32::try_from(line.len()).expect("Too many cols for i32");
for (col, c) in line.chars().enumerate() {
let signed_col = i32::try_from(col).expect("Too many rolumns for i32");
if c != '.' {
antennas.entry(c).or_insert(Vec::new()).push(Point {
row: signed_row,
col: signed_col,
});
}
}
}
Grid {
n_rows,
n_cols,
antennas,
}
}
pub fn task(input: &str, with_harmonics: bool) -> String {
let grid = parse_input(input);
let result = grid.get_antinodes(with_harmonics);
result.len().to_string()
}
pub fn task01(input: &str) -> String {
task(input, false)
}
pub fn task02(input: &str) -> String {
task(input, true)
}
#[cfg(test)]
mod tests {
use super::super::fs_utils::{read_example, read_input};
use super::*;
#[test]
fn test_task01() {
let input = read_example(8, 1);
assert_eq!(task01(&input), "14");
}
#[test]
fn run_task01() {
let input = read_input(8);
assert_eq!(task01(&input), "359");
}
#[test]
fn test_task02() {
let input = read_example(8, 1);
assert_eq!(task02(&input), "34");
}
#[test]
fn run_task02() {
let input = read_input(8);
assert_eq!(task02(&input), "1293");
}
}