|
1 | | -// use aoc_runner_derive::{aoc, aoc_generator}; |
| 1 | +use aoc_runner_derive::{aoc, aoc_generator}; |
| 2 | +use std::collections::{HashMap, HashSet}; |
2 | 3 |
|
3 | | -// #[aoc_generator(day8)] |
4 | | -// pub fn input_generator(input: &str) -> (Vec<usize>, Vec<usize>) { |
| 4 | +#[derive(Debug)] |
| 5 | +pub struct City { |
| 6 | + frequencies: HashMap<char, HashSet<(usize, usize)>>, |
| 7 | + y_len: usize, |
| 8 | + x_len: usize, |
| 9 | +} |
5 | 10 |
|
6 | | -// #[aoc(day8, part1)] |
7 | | -// pub fn part1(input: &[Equation]) -> usize { |
| 11 | +#[aoc_generator(day8)] |
| 12 | +pub fn input_generator(input: &str) -> City { |
| 13 | + let mut frequencies: HashMap<char, HashSet<(usize, usize)>> = HashMap::new(); |
| 14 | + let map: Vec<Vec<char>> = input |
| 15 | + .lines() |
| 16 | + .enumerate() |
| 17 | + .map(|(y, l)| { |
| 18 | + l.chars() |
| 19 | + .enumerate() |
| 20 | + .map(|(x, c)| { |
| 21 | + if c != '.' { |
| 22 | + frequencies.entry(c).or_default().insert((y, x)); |
| 23 | + } |
| 24 | + c |
| 25 | + }) |
| 26 | + .collect() |
| 27 | + }) |
| 28 | + .collect(); |
| 29 | + City { |
| 30 | + y_len: map.len(), |
| 31 | + x_len: map[0].len(), |
| 32 | + frequencies, |
| 33 | + } |
| 34 | +} |
8 | 35 |
|
9 | | -// #[aoc(day8, part2)] |
10 | | -// pub fn part2(input: &[Equation]) -> usize { |
| 36 | +#[aoc(day8, part1)] |
| 37 | +pub fn part1(input: &City) -> usize { |
| 38 | + let mut antinodes: HashSet<(usize, usize)> = HashSet::new(); |
| 39 | + let mut count = 0; |
| 40 | + for positions in input.frequencies.values() { |
| 41 | + for current_pos in positions { |
| 42 | + for around_pos in positions { |
| 43 | + if current_pos == around_pos { |
| 44 | + continue; |
| 45 | + } |
| 46 | + let y = (current_pos.0 * 2).wrapping_sub(around_pos.0); |
| 47 | + let x = (current_pos.1 * 2).wrapping_sub(around_pos.1); |
| 48 | + if y < input.y_len && x < input.x_len && !antinodes.contains(&(y, x)) { |
| 49 | + count += 1; |
| 50 | + antinodes.insert((y, x)); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + count |
| 56 | +} |
| 57 | + |
| 58 | +#[aoc(day8, part2)] |
| 59 | +pub fn part2(input: &City) -> usize { |
| 60 | + let mut antinodes: HashSet<(usize, usize)> = HashSet::new(); |
| 61 | + let mut count = 0; |
| 62 | + for positions in input.frequencies.values() { |
| 63 | + for current_pos in positions { |
| 64 | + for around_pos in positions { |
| 65 | + if current_pos == around_pos { |
| 66 | + continue; |
| 67 | + } |
| 68 | + let delta_y: isize = current_pos.0 as isize - around_pos.0 as isize; |
| 69 | + let delta_x: isize = current_pos.1 as isize - around_pos.1 as isize; |
| 70 | + |
| 71 | + let mut y: isize = around_pos.0 as isize; |
| 72 | + let mut x: isize = around_pos.1 as isize; |
| 73 | + loop { |
| 74 | + y -= delta_y; |
| 75 | + x -= delta_x; |
| 76 | + if x < 0 || y < 0 || x >= input.x_len as isize || y >= input.y_len as isize { |
| 77 | + break; |
| 78 | + } |
| 79 | + if !antinodes.contains(&(y as usize, x as usize)) { |
| 80 | + count += 1; |
| 81 | + antinodes.insert((y as usize, x as usize)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + y = around_pos.0 as isize; |
| 86 | + x = around_pos.1 as isize; |
| 87 | + loop { |
| 88 | + y = y.wrapping_add(delta_y); |
| 89 | + x = x.wrapping_add(delta_x); |
| 90 | + if x < 0 || y < 0 || x >= input.x_len as isize || y >= input.y_len as isize { |
| 91 | + break; |
| 92 | + } |
| 93 | + if !antinodes.contains(&(y as usize, x as usize)) { |
| 94 | + count += 1; |
| 95 | + antinodes.insert((y as usize, x as usize)); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + count |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + use super::*; |
| 108 | + |
| 109 | + const INPUT: &str = "............ |
| 110 | +........0... |
| 111 | +.....0...... |
| 112 | +.......0.... |
| 113 | +....0....... |
| 114 | +......A..... |
| 115 | +............ |
| 116 | +............ |
| 117 | +........A... |
| 118 | +.........A.. |
| 119 | +............ |
| 120 | +............ |
| 121 | +"; |
| 122 | + // const INPUT: &str = "T......... |
| 123 | + // ...T...... |
| 124 | + // .T........ |
| 125 | + // .......... |
| 126 | + // .......... |
| 127 | + // .......... |
| 128 | + // .......... |
| 129 | + // .......... |
| 130 | + // .......... |
| 131 | + // .........."; |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_part1() { |
| 135 | + assert_eq!(14, part1(&input_generator(INPUT))) |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn test_part2() { |
| 140 | + assert_eq!(34, part2(&input_generator(INPUT))) |
| 141 | + } |
| 142 | +} |
0 commit comments