|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use crate::utils; |
| 4 | + |
| 5 | +pub fn solve_part1(input: &str) -> i32 { |
| 6 | + solve(input, false) |
| 7 | +} |
| 8 | + |
| 9 | +pub fn solve_part2(input: &str) -> i32 { |
| 10 | + solve(input, true) |
| 11 | +} |
| 12 | + |
| 13 | +fn solve(input: &str, part2: bool) -> i32 { |
| 14 | + let map = utils::parse_string_to_2d_vector(input); |
| 15 | + let mut locations: HashSet<(usize, usize)> = HashSet::new(); |
| 16 | + for (y, row) in map.iter().enumerate() { |
| 17 | + for (x, c) in row.iter().enumerate() { |
| 18 | + if is_antenna(*c) { |
| 19 | + locations = locations |
| 20 | + .union(&get_antidode_locations(&map, *c, (x, y), part2)) |
| 21 | + .cloned() |
| 22 | + .collect(); |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + locations.len().try_into().unwrap() |
| 28 | +} |
| 29 | + |
| 30 | +fn get_antidode_locations( |
| 31 | + map: &[Vec<char>], |
| 32 | + c: char, |
| 33 | + start_pos: (usize, usize), |
| 34 | + many_antidodes: bool, |
| 35 | +) -> HashSet<(usize, usize)> { |
| 36 | + let (x, y) = start_pos; |
| 37 | + let ry = get_radius_per_dimension(map.len(), y); |
| 38 | + let rx = get_radius_per_dimension(map.first().unwrap().len(), x); |
| 39 | + |
| 40 | + let mut locations: HashSet<(usize, usize)> = HashSet::new(); |
| 41 | + for y_prime in y - ry..=y + ry { |
| 42 | + for x_prime in x - rx..=x + rx { |
| 43 | + if *map.get(y_prime).unwrap().get(x_prime).unwrap() == c && y_prime != y && x_prime != x |
| 44 | + { |
| 45 | + let antidode = mirror_point_through((x_prime, y_prime), start_pos); |
| 46 | + locations.insert(antidode); |
| 47 | + if many_antidodes { |
| 48 | + locations.insert(start_pos); |
| 49 | + locations.insert((x_prime, y_prime)); |
| 50 | + |
| 51 | + let (vec_x, vec_y) = |
| 52 | + (antidode.0 as i32 - x as i32, antidode.1 as i32 - y as i32); |
| 53 | + let mut i = 1; |
| 54 | + loop { |
| 55 | + let y_prime_prime = (antidode.1 as i32 + i * vec_y) as usize; |
| 56 | + let x_prime_prime = (antidode.0 as i32 + i * vec_x) as usize; |
| 57 | + |
| 58 | + if map |
| 59 | + .get(y_prime_prime) |
| 60 | + .and_then(|row| row.get(x_prime_prime)) |
| 61 | + .is_none() |
| 62 | + { |
| 63 | + break; |
| 64 | + } |
| 65 | + |
| 66 | + locations.insert((x_prime_prime, y_prime_prime)); |
| 67 | + i += 1; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + locations |
| 75 | +} |
| 76 | + |
| 77 | +fn mirror_point_through(point: (usize, usize), through: (usize, usize)) -> (usize, usize) { |
| 78 | + let (x1, y1) = through; |
| 79 | + let (x2, y2) = point; |
| 80 | + |
| 81 | + let mirrored_x = 2 * x1 - x2; |
| 82 | + let mirrored_y = 2 * y1 - y2; |
| 83 | + |
| 84 | + (mirrored_x, mirrored_y) |
| 85 | +} |
| 86 | + |
| 87 | +fn get_radius_per_dimension(length: usize, coordinate: usize) -> usize { |
| 88 | + if coordinate >= (length / 2) { |
| 89 | + return length - coordinate - 1; |
| 90 | + } |
| 91 | + |
| 92 | + coordinate |
| 93 | +} |
| 94 | + |
| 95 | +fn is_antenna(c: char) -> bool { |
| 96 | + c != '.' |
| 97 | +} |
| 98 | + |
| 99 | +#[cfg(test)] |
| 100 | +mod tests { |
| 101 | + use super::*; |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn test_solve_part1() { |
| 105 | + let input = "............ |
| 106 | +........0... |
| 107 | +.....0...... |
| 108 | +.......0.... |
| 109 | +....0....... |
| 110 | +......A..... |
| 111 | +............ |
| 112 | +............ |
| 113 | +........A... |
| 114 | +.........A.. |
| 115 | +............ |
| 116 | +............"; |
| 117 | + assert_eq!(solve_part1(input), 14); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_solve_part2() { |
| 122 | + let input_1 = "............ |
| 123 | +........0... |
| 124 | +.....0...... |
| 125 | +.......0.... |
| 126 | +....0....... |
| 127 | +......A..... |
| 128 | +............ |
| 129 | +............ |
| 130 | +........A... |
| 131 | +.........A.. |
| 132 | +............ |
| 133 | +............"; |
| 134 | + assert_eq!(solve_part2(input_1), 34); |
| 135 | + |
| 136 | + let input_2 = "T......... |
| 137 | +...T...... |
| 138 | +.T........ |
| 139 | +.......... |
| 140 | +.......... |
| 141 | +.......... |
| 142 | +.......... |
| 143 | +.......... |
| 144 | +.......... |
| 145 | +.........."; |
| 146 | + assert_eq!(solve_part2(input_2), 9); |
| 147 | + } |
| 148 | +} |
0 commit comments