diff --git a/2024/08/Cargo.toml b/2024/08/Cargo.toml new file mode 100644 index 0000000..b6c476f --- /dev/null +++ b/2024/08/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "aoc-2024-08" +version = "0.1.0" +authors = ["MÃ¥rten Kongstad "] +edition = "2021" + +[dependencies] +anyhow = "1.0" +aoc = { path = "../../aoc" } diff --git a/2024/08/src/input.txt b/2024/08/src/input.txt new file mode 100644 index 0000000..8430fd1 --- /dev/null +++ b/2024/08/src/input.txt @@ -0,0 +1,50 @@ +...........6.b.................................... +........6................8........................ +..Y.......................................o....... +....V...j............B.............c.............. +............8.........X.......L................... +.....j..v6.......3.L..................c........... +..Mj.....p3.......b........Z....................J. +..........M...X................................... +V..............v......p.........Z.........c....... +..............3................................... +.......V......U3.............c.................... +..........b..v.M.U8............................... +..........j........8.....................J........ +..........Y......q........LH..Z...D...........y... +..2Y........PX......6..................BQ......... +...0.Y...............XP...........w............... +.........U.......2...............oH.y............. +0..............9........U......................... +...........P..............W.......z...Oy.......... +...................t...p.W..o.............Q....... +.....S.................t.....Q....B............... +S.k..................V..W...p.......H...O......m.. +....S.h................W.......................O.. +..h..P.2.............Z.............J.............. +.........k.......5v.......q...t.s................. +.....Q.....h..........................J...B....... +........0.........l............................... +.S................................................ +.............................M.................... +2..................e.....o.....y.................. +................k................................. +......4......k....t...s.q......................... +.4.......................q........................ +.......................z....E..................... +.............0.....d.............................. +7..........D........z............................. +.......D..5......7..9............................. +......5..................E........................ +D..............K......d..9E..........w.....1..C... +.......K..x.........d....s...........l............ +........7......................u...C.............. +..K........x..............9..C...u................ +4..............s.........................l...T..w. +.......5.....7..................m......T......1... +...........................E...z.m................ +......................................u...C....... +.............................em................... +..............................................T... +....................x.......................e..... +.............................1e....w....l......... diff --git a/2024/08/src/main.rs b/2024/08/src/main.rs new file mode 100644 index 0000000..c674e8a --- /dev/null +++ b/2024/08/src/main.rs @@ -0,0 +1,81 @@ +use anyhow::{bail, Result}; +use aoc::{BoundingBox, XY}; +use std::collections::{HashMap, HashSet}; + +fn main() -> Result<()> { + let input = include_str!("input.txt"); + aoc::run!(part_one(input), 305)?; + aoc::run!(part_two(input), 1150)?; + Ok(()) +} + +type AntennaMap = HashMap>; + +fn parse(input: &str) -> Result<(AntennaMap, BoundingBox)> { + let mut antennas: AntennaMap = HashMap::new(); + let bounding_box = aoc::parse_grid(input, |xy, ch| match ch { + '.' => Ok(()), + ch if ch.is_ascii_alphanumeric() => { + antennas.entry(ch).or_default().push(xy); + Ok(()) + } + _ => bail!("unexpected char {ch}"), + })?; + Ok((antennas, bounding_box)) +} + +fn part_x(input: &str, part_two_specifics: bool) -> Result { + let (antennas, bb) = parse(input)?; + let mut signals: HashSet = HashSet::new(); + for v in antennas.values() { + for a in v { + if part_two_specifics { + signals.insert(*a); + } + for b in v { + if a == b { + continue; + } + let step = a - b; + let mut xy = *a; + loop { + xy += step; + if bb.contains(&xy) { + signals.insert(xy); + } else { + break; + } + if !part_two_specifics { + break; + } + } + } + } + } + Ok(signals.len()) +} + +fn part_one(input: &str) -> Result { + part_x(input, false) +} + +fn part_two(input: &str) -> Result { + part_x(input, true) +} + +#[cfg(test)] +mod tests { + use super::*; + + const INPUT: &str = include_str!("test-input.txt"); + + #[test] + fn test_part_one() { + assert_eq!(part_one(INPUT).unwrap(), 14); + } + + #[test] + fn test_part_two() { + assert_eq!(part_two(INPUT).unwrap(), 34); + } +} diff --git a/2024/08/src/test-input.txt b/2024/08/src/test-input.txt new file mode 100644 index 0000000..78a1e91 --- /dev/null +++ b/2024/08/src/test-input.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ diff --git a/Cargo.lock b/Cargo.lock index 0ca6f89..06c63a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -877,6 +877,14 @@ dependencies = [ "aoc", ] +[[package]] +name = "aoc-2024-08" +version = "0.1.0" +dependencies = [ + "anyhow", + "aoc", +] + [[package]] name = "ascii" version = "1.1.0"