Skip to content

Commit

Permalink
AOC 2024 day 08
Browse files Browse the repository at this point in the history
Signed-off-by: Mårten Kongstad <[email protected]>
  • Loading branch information
amhk committed Dec 9, 2024
1 parent 0033426 commit a001372
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 2024/08/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "aoc-2024-08"
version = "0.1.0"
authors = ["Mårten Kongstad <[email protected]>"]
edition = "2021"

[dependencies]
anyhow = "1.0"
aoc = { path = "../../aoc" }
50 changes: 50 additions & 0 deletions 2024/08/src/input.txt
Original file line number Diff line number Diff line change
@@ -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.........
81 changes: 81 additions & 0 deletions 2024/08/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<char, Vec<XY>>;

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<usize> {
let (antennas, bb) = parse(input)?;
let mut signals: HashSet<XY> = 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<usize> {
part_x(input, false)
}

fn part_two(input: &str) -> Result<usize> {
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);
}
}
12 changes: 12 additions & 0 deletions 2024/08/src/test-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a001372

Please sign in to comment.