Solve day 8

This commit is contained in:
hal8174 2024-12-08 14:16:32 +01:00
parent 9981ef632a
commit 695e0b6fb8
3 changed files with 158 additions and 0 deletions

12
input/08-0.in Normal file
View file

@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

50
input/08-1.in Normal file
View file

@ -0,0 +1,50 @@
..................................3.........H.....
..............F............................CK.....
..................F...e...m..........C.Ki.........
......1..................m........................
.........F.1......................................
.......1.....W...........3..Z............i........
...........W...m....1.............................
......W...........m..............N..C.............
E............2................Z.K.......p.........
.....Y.........4....i.........N...................
..............W.Y....................3..9....i....
.................................h........9.......
.........................................Z.......H
2...............................3.......H9........
..2..........4T...................................
...2..............Y...4........Z..................
.........E.........................N...5..........
.......................................e..........
..............................C...................
..E..................P.....................p.H....
......4............................IN......h.p....
..........................T....M.........K..p.....
..........................G.......................
..................................................
...................................M..............
.5.............G..............M...................
.............Y..........................M.........
.................E8...0.........................h.
.............................P............g.......
......5...........................................
.............n.................................c..
...............................g....f.......c.....
.y..............8...t....T........................
..7..F.............T........R..........f........u.
.kz.......7..R....................................
.........8..................U.........P...........
......U.............wG....v.....P.............c...
...0.....R..........g.............................
.....7.....8.........g.............f..............
....z...........G................7................
........5........6.v.....U..f.......u........e....
.........V....v........6......t...................
......6..0..y.....R........V...........r..........
...........v.......we..U.............c..r.........
................................r.......Iu........
k............y6..........t.................r...I..
........k............t...........w................
.............z....n.................I.............
..0.................n.............................
...............n..........V...........y........u..

96
src/bin/08.rs Normal file
View file

@ -0,0 +1,96 @@
use std::collections::{hash_map::Entry, HashMap, HashSet};
fn main() {
let text = std::fs::read_to_string("input/08-1.in").unwrap();
let mut antinodes = HashSet::new();
let mut antenna: HashMap<char, Vec<(usize, usize)>> = HashMap::new();
let mut height = 0;
let mut width = 0;
for (x, y, char) in text
.lines()
.enumerate()
.inspect(|(y, _)| height = height.max(y + 1))
.inspect(|(_, l)| width = l.len())
.flat_map(|(y, l)| {
l.chars()
.enumerate()
.map(move |(x, c)| (x, y, c))
.filter(|(_, _, c)| c != &'.')
})
{
match antenna.entry(char) {
Entry::Occupied(mut e) => {
for (other_x, other_y) in e.get() {
if let Some((antinode_x, antinode_y)) =
Option::zip((2 * x).checked_sub(*other_x), (2 * y).checked_sub(*other_y))
{
antinodes.insert((antinode_x, antinode_y));
}
if let Some((antinode_x, antinode_y)) =
Option::zip((2 * other_x).checked_sub(x), (2 * other_y).checked_sub(y))
{
antinodes.insert((antinode_x, antinode_y));
}
}
e.get_mut().push((x, y));
}
Entry::Vacant(e) => {
e.insert(vec![(x, y)]);
}
}
}
let t = antinodes
.iter()
.filter(|(x, y)| x < &width && y < &height)
.count();
println!("{}", t);
let mut antinodes = HashSet::new();
let mut antenna: HashMap<char, Vec<(usize, usize)>> = HashMap::new();
for (x, y, char) in text.lines().enumerate().flat_map(|(y, l)| {
l.chars()
.enumerate()
.map(move |(x, c)| (x, y, c))
.filter(|(_, _, c)| c != &'.')
}) {
match antenna.entry(char) {
Entry::Occupied(mut e) => {
for (other_x, other_y) in e.get() {
let dir_x = *other_x as isize - x as isize;
let dir_y = *other_y as isize - y as isize;
let mut i = 1;
while let Some((antinode_x, antinode_y)) = Option::zip(
x.checked_add_signed(dir_x * i).filter(|&x| x < width),
y.checked_add_signed(dir_y * i).filter(|&y| y < height),
) {
antinodes.insert((antinode_x, antinode_y));
i += 1;
}
let mut i = 0;
while let Some((antinode_x, antinode_y)) = Option::zip(
x.checked_add_signed(dir_x * i).filter(|&x| x < width),
y.checked_add_signed(dir_y * i).filter(|&y| y < height),
) {
antinodes.insert((antinode_x, antinode_y));
i -= 1;
}
}
e.get_mut().push((x, y));
}
Entry::Vacant(e) => {
e.insert(vec![(x, y)]);
}
}
}
println!("{}", antinodes.len());
}