|
| 1 | +use std::collections::{HashMap, HashSet}; |
| 2 | + |
| 3 | +use aoc_lib::matrix::Matrix; |
| 4 | +use common::{solution, Answer}; |
| 5 | +use nd_vec::{vector, Vec2}; |
| 6 | + |
| 7 | +use itertools::Itertools; |
| 8 | + |
| 9 | +solution!("Resonant Collinearity", 8); |
| 10 | + |
| 11 | +#[derive(Clone)] |
| 12 | +enum Tile { |
| 13 | + Emitter(char), |
| 14 | + Empty, |
| 15 | +} |
| 16 | + |
| 17 | +fn part_a(input: &str) -> Answer { |
| 18 | + let world = Matrix::new_chars(input, |x| match x { |
| 19 | + 'a'..='z' | 'A'..='Z' | '0'..='9' => Tile::Emitter(x), |
| 20 | + _ => Tile::Empty, |
| 21 | + }); |
| 22 | + |
| 23 | + let mut freqs = HashMap::<char, Vec<Vec2<i32>>>::new(); |
| 24 | + |
| 25 | + // oh god just make a Matrix.iter function at this point... |
| 26 | + for x in 0..world.size.x() { |
| 27 | + for y in 0..world.size.y() { |
| 28 | + let pos = vector!(x, y); |
| 29 | + if let Tile::Emitter(chr) = world.get(pos).unwrap() { |
| 30 | + freqs |
| 31 | + .entry(*chr) |
| 32 | + .or_default() |
| 33 | + .push(pos.try_cast::<i32>().unwrap()); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // nodes are on the line between two of the same freq emitters when d1 = 2*d2 |
| 39 | + // to find nodes, start by finding all lines through matching emitters |
| 40 | + |
| 41 | + let in_bounds = |pos: Vec2<i32>| { |
| 42 | + pos.x() >= 0 |
| 43 | + && pos.y() >= 0 |
| 44 | + && pos.x() < world.size.x() as i32 |
| 45 | + && pos.y() < world.size.y() as i32 |
| 46 | + }; |
| 47 | + |
| 48 | + let mut out = HashSet::new(); |
| 49 | + for (_freq, pos) in freqs { |
| 50 | + for (a, b) in pos.into_iter().tuple_combinations() { |
| 51 | + let diff_a = a + (a - b); |
| 52 | + let diff_b = b + (b - a); |
| 53 | + |
| 54 | + if in_bounds(diff_a) { |
| 55 | + out.insert(diff_a); |
| 56 | + } |
| 57 | + |
| 58 | + if in_bounds(diff_b) { |
| 59 | + out.insert(diff_b); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + out.len().into() |
| 65 | +} |
| 66 | + |
| 67 | +fn part_b(input: &str) -> Answer { |
| 68 | + let world = Matrix::new_chars(input, |x| match x { |
| 69 | + 'a'..='z' | 'A'..='Z' | '0'..='9' => Tile::Emitter(x), |
| 70 | + _ => Tile::Empty, |
| 71 | + }); |
| 72 | + |
| 73 | + let mut freqs = HashMap::<char, Vec<Vec2<i32>>>::new(); |
| 74 | + |
| 75 | + // oh god just make a Matrix.iter function at this point... |
| 76 | + for x in 0..world.size.x() { |
| 77 | + for y in 0..world.size.y() { |
| 78 | + let pos = vector!(x, y); |
| 79 | + if let Tile::Emitter(chr) = world.get(pos).unwrap() { |
| 80 | + freqs |
| 81 | + .entry(*chr) |
| 82 | + .or_default() |
| 83 | + .push(pos.try_cast::<i32>().unwrap()); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // nodes are on the line between two of the same freq emitters when d1 = 2*d2 |
| 89 | + // to find nodes, start by finding all lines through matching emitters |
| 90 | + |
| 91 | + let in_bounds = |pos: Vec2<i32>| { |
| 92 | + pos.x() >= 0 |
| 93 | + && pos.y() >= 0 |
| 94 | + && pos.x() < world.size.x() as i32 |
| 95 | + && pos.y() < world.size.y() as i32 |
| 96 | + }; |
| 97 | + |
| 98 | + let mut out = HashSet::new(); |
| 99 | + for (_freq, pos) in freqs { |
| 100 | + for (a, b) in pos.into_iter().tuple_combinations() { |
| 101 | + let diff_a = a - b; |
| 102 | + let diff_b = b - a; |
| 103 | + |
| 104 | + let mut pos_a = a; |
| 105 | + let mut pos_b = b; |
| 106 | + |
| 107 | + while in_bounds(pos_a) { |
| 108 | + out.insert(pos_a); |
| 109 | + pos_a += diff_a; |
| 110 | + } |
| 111 | + |
| 112 | + while in_bounds(pos_b) { |
| 113 | + out.insert(pos_b); |
| 114 | + pos_b += diff_b; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + out.len().into() |
| 120 | +} |
| 121 | + |
| 122 | +#[cfg(test)] |
| 123 | +mod test { |
| 124 | + use indoc::indoc; |
| 125 | + |
| 126 | + const CASE: &str = indoc! {" |
| 127 | + ............ |
| 128 | + ........0... |
| 129 | + .....0...... |
| 130 | + .......0.... |
| 131 | + ....0....... |
| 132 | + ......A..... |
| 133 | + ............ |
| 134 | + ............ |
| 135 | + ........A... |
| 136 | + .........A.. |
| 137 | + ............ |
| 138 | + ............ |
| 139 | + "}; |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn part_a() { |
| 143 | + assert_eq!(super::part_a(CASE), 14.into()); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn part_b() { |
| 148 | + assert_eq!(super::part_b(CASE), 34.into()); |
| 149 | + } |
| 150 | +} |
0 commit comments