From 1568dc448423dd86bce2eb84219b17b2e0072e94 Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Sun, 8 Dec 2024 15:23:10 +0100 Subject: 2024.8 --- 2024_rust/inputs/8 | 50 +++++++++++++++++ 2024_rust/src/bin/day8.rs | 138 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 2024_rust/inputs/8 create mode 100644 2024_rust/src/bin/day8.rs diff --git a/2024_rust/inputs/8 b/2024_rust/inputs/8 new file mode 100644 index 0000000..6d6f666 --- /dev/null +++ b/2024_rust/inputs/8 @@ -0,0 +1,50 @@ +..........1.............TE........................ +....................................R............. +.................................................. +.......................j.....Q.................... +...................A................8............. +...........................s.......9...........k.. +q.E..............6...............1R.w.........k... +..6...E..............1.........R...............t.. +.....r.Q......6........Re..T..............9....... +.............................T........9........... +...............................................wv. +.P............A..................8.v....s.k....... +.q..................A......k.........8............ +..........o.....1.....W..H............8.......w... +..Q........P.........O.........e...N.W............ +P................z.........o.............N.......w +..............o.....p..........Z.s..........N..... +.....O.x......K.....................v..aN......... +..O...............U.....H.......t................. +.E.......q...6.....i.............................. +..............z..........o...i...........aW....... +....O........r.............e.....Wt............... +...............U.7i........H......h........t...... +......Q.......n..2...I...A....i.p................. +...........2...9n.................s........j...... +..q................Ur..........p.................. +.............n.................K.................. +.....S....z.........I.....H.............e.j....... +..................7..prD..K...d................... +S.........V.....7....K............................ +......................................0........... +.................................................. +..................2..........I....j.Z............. +....................X.............J..Z....a....... +........SX............................x......0J... +................U....n........x...............0... +.........S......X................x....a........... +...5.......X.......................02............. +...............V.........................d...J.... +.............................u.......4............ +.....5...........................u.4.............. +....5............................................. +......V................................3.......... +......D..........................................d +....D.................................4........... +.....h....................................d7...... +..............................P................... +.........D......h........3................u...4... +.............h..5.....3...........u.....I......... +..........3......V.............................J.. diff --git a/2024_rust/src/bin/day8.rs b/2024_rust/src/bin/day8.rs new file mode 100644 index 0000000..cbdfb77 --- /dev/null +++ b/2024_rust/src/bin/day8.rs @@ -0,0 +1,138 @@ +use aoc2024::matrix; +use itertools::Itertools; +use std::collections::{HashMap, HashSet}; +use std::fmt; + +type Freq = char; + +enum Dot { + Empty, + Antenna(Freq), +} +use Dot::*; + +impl fmt::Display for Dot { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let ch = match self { + Empty => &'.', + Antenna(c) => c, + }; + write!(f, "{}", ch) + } +} + +impl Dot { + fn parse(c: char) -> Dot { + match c { + '.' => Empty, + c => Antenna(c), + } + } +} + +struct M { + matrix: matrix::Matrix, + antennas: HashMap>, + antinodes: HashSet, +} + +impl M { + fn new(text: &str) -> Self { + let matrix = matrix::Matrix::new(text, Dot::parse); + let mut antennas: HashMap> = HashMap::new(); + for i in 0..matrix.limit.0 { + for j in 0..matrix.limit.1 { + match matrix.get((i, j)) { + Empty => (), + Antenna(freq) => { + antennas.entry(*freq).or_default().insert((i, j)); + } + } + } + } + M { + matrix, + antennas, + antinodes: HashSet::new(), + } + } + + fn compute_antinodes(&mut self, version: u8) { + for (_freq, antennas) in self.antennas.iter() { + // println!("Freq {freq}: {antennas:?}"); + for (a1, a2) in antennas + .into_iter() + .permutations(2) + .map(|x| x.into_iter().collect_tuple().unwrap()) + { + match version { + 0 => { + let an = antinode(a1, a2, &self.matrix.limit); + // println!(" ({a1:?}, {a2:?}) -> {an:?}"); + if let Some((x, y)) = an { + self.antinodes.insert((x, y)); + } + } + 1 => { + for an in antinodes(a1, a2, &self.matrix.limit) { + self.antinodes.insert(an); + } + } + _ => panic!(), + } + } + } + // println!(" {:?}", self.antinodes); + } +} + +fn antinodes(a1: &matrix::Pos, a2: &matrix::Pos, limit: &matrix::Pos) -> Vec { + let mut result = vec![*a1, *a2]; + let mut a: matrix::Pos = result[0]; + let mut b: matrix::Pos = result[1]; + while let Some(an) = antinode(&a, &b, limit) { + result.push(an.clone()); + a = result[result.len() - 2]; + b = result[result.len() - 1]; + } + result +} + +fn antinode(a1: &matrix::Pos, a2: &matrix::Pos, limit: &matrix::Pos) -> Option { + let (x1, y1) = (a1.0 as isize, a1.1 as isize); + let (x2, y2) = (a2.0 as isize, a2.1 as isize); + let x = x2 - (x1 - x2); + let y = y2 - (y1 - y2); + if y >= 0 && x >= 0 && y < limit.1 as isize && x < limit.0 as isize { + Some((x as usize, y as usize)) + } else { + None + } +} + +impl fmt::Display for M { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // write!(f, "{}[2J", 27 as char)?; + write!(f, "{}", self.matrix.to_string()) + } +} + +fn p1(input: &str) -> String { + let mut m: M = M::new(input); + m.compute_antinodes(0); + + let result = m.antinodes.len(); + result.to_string() +} + +fn p2(input: &str) -> String { + let mut m: M = M::new(input); + m.compute_antinodes(1); + + let result = m.antinodes.len(); + result.to_string() +} + +fn main() { + aoc2024::run_day("8", p1, p2); +} -- cgit v1.2.3