}
}
-fn run_part1(input: &str) -> Result<u32, Box<dyn Error>> {
- println!("Running {} - part 1", get_day());
-
- let puzzle = Puzzle::new(input);
+fn solve(puzzle: &Puzzle, depth: &[isize]) -> u32 {
let mut visited: HashSet<(isize, isize)> = HashSet::new();
let mut antinodes: HashSet<(isize, isize)> = HashSet::new();
puzzle.map.iter()
.filter(|(k, v)| *v == value && *k != key)
.for_each(|(k, v)| {
- let x1 = if k <= key { k } else { key };
- let x2 = if k <= key { key } else { k };
+ let (x1, x2) = if k <= key { (k, key) } else { (key, k) };
let d = (x1.0 - x2.0, x1.1 - x2.1);
- for a in [(x1.0 + d.0, x1.1 + d.1), (x2.0 - d.0, x2.1 - d.1)] {
- if puzzle.map.contains_key(&a) {
- antinodes.insert(a);
+ for i in depth {
+ for a in [(x1.0 + i*d.0, x1.1 + i*d.1), (x2.0 - i*d.0, x2.1 - i*d.1)] {
+ if puzzle.map.contains_key(&a) {
+ antinodes.insert(a);
+ }
}
}
});
}
});
- let res = antinodes.len() as u32;
- Ok(res)
+ antinodes.len() as u32
}
-fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
- println!("Running {} - part 2", get_day());
- let puzzle = Puzzle::new(input);
- let mut visited: HashSet<(isize, isize)> = HashSet::new();
- let mut antinodes: HashSet<(isize, isize)> = HashSet::new();
+fn run_part1(input: &str) -> Result<u32, Box<dyn Error>> {
+ println!("Running {} - part 1", get_day());
- puzzle.map.iter()
- .filter(|(_, value)| **value != '.')
- .for_each(|(key, value)| {
- if !visited.contains(key) {
- visited.insert(*key);
+ let puzzle = Puzzle::new(input);
+ Ok(solve(&puzzle, &[1]))
+}
- puzzle.map.iter()
- .filter(|(k, v)| *v == value && *k != key)
- .for_each(|(k, v)| {
- let x1 = if k <= key { k } else { key };
- let x2 = if k <= key { key } else { k };
- let d = (x1.0 - x2.0, x1.1 - x2.1);
- for i in 0..puzzle.map.len() {
- let i = i as isize;
- for a in [(x1.0 + i*d.0, x1.1 + i*d.1), (x2.0 - i*d.0, x2.1 - i*d.1)] {
- if puzzle.map.contains_key(&a) {
- antinodes.insert(a);
- }
- }
- }
- });
- }
- });
+fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
+ println!("Running {} - part 2", get_day());
- let res = antinodes.len() as u32;
- Ok(res)
+ let puzzle = Puzzle::new(input);
+ let depth = (0..puzzle.map.len()).map(|v| v as isize).collect::<Vec<isize>>();
+ Ok(solve(&puzzle, &depth))
}
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {