commit db13595fbfe221cdbdfbe5dd8134ed28f25c8f71 parent 1af7fd644bbac6ad0c08078a59d37b095fbeca10 Author: Lukas Henkel <lh@entf.net> Date: Sun, 8 Dec 2024 06:48:58 +0100 Day 8 Diffstat:
A | input/8.txt | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | src/day-8.lisp | | | 54 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | t/day-8.lisp | | | 22 | ++++++++++++++++++++++ |
3 files changed, 126 insertions(+), 0 deletions(-)
diff --git a/input/8.txt b/input/8.txt @@ -0,0 +1,50 @@ +....................................8............. +..................E............................... +.................................g................ +...........................................l...b.. +..C...........s..............8..........b......... +..................3..1........................b... +............N....3.....................1.....b.... +.....................N.....8....1..............2.. +..q....................................P.......... +......................N........................... +...........E.................................l.... +.............S.....c.............T..2v............ +.........w....E........q............L.....P.....l. +........w..............................a...V...... +...........t..................v..V................ +.....w.C............................V....4.....L.. +........................................I.n..T.... +.....E.5..C...8....3..q........................... +...............s..0...A........W...........a....T. +...............A................vPT...L..W..e.4... +...........Cw..................2.....G.p.....4.... +....S........q........s.............a............. +S.............c......e....................V....... +......5........................................... +....5............................................. +...........................I............g......... +...............c.........A........................ +.................s.............G.............etg.. +.........5...L.........f...v......W............... +............................0.W.....I........t.... +.................................................. +...................f...........Q.0................ +..............1m9.f..........0........3.........F. +..f...9................B.......................... +...........S...........................F......e... +........c.............n.....Q..................... +.....N...............B............g..7....t....... +..........B.........P.......G..................... +..m...........................Q................... +.............m.....................p...........F.. +.....M..B......Q..i.....................7.4....... +............M..................7.................. +...........n...................................... +................................p.....6.F.7....... +..........M...........p.........6................. +.M............i................................... +..............................G................... +..............li.......................6.......... +.....9.....................i...6.................. +.....n.............................9.............. diff --git a/src/day-8.lisp b/src/day-8.lisp @@ -0,0 +1,54 @@ +(defpackage #:aoc/day-8 + (:use #:cl #:aoc/utils) + (:export #:day-8)) +(in-package #:aoc/day-8) + +(defun find-antennas (map) + (loop with antennas = (make-hash-table) + for x from 0 below (input-map-width map) + do (loop for y from 0 below (input-map-height map) + for point = (cons x y) + for cell = (map-cell map point) + unless (char= cell #\.) + do (setf (gethash cell antennas) + (cons point (gethash cell antennas)))) + finally (return antennas))) + +(defun antinode-location (location-1 location-2) + (when (equal location-1 location-2) + (return-from antinode-location nil)) + (point+ location-1 (point- location-1 location-2))) + +(defun antinode-locations (location-1 location-2 width height) + (when (equal location-1 location-2) + (return-from antinode-locations nil)) + (loop with diff = (point- location-1 location-2) + for point = (point+ (or point location-1) diff) + while (point-in-bounds-p point width height) + collect point)) + +(defun compute-antinodes (map antennas) + (loop with antinodes-task-1 = (make-hash-table :test #'equal) + with antinodes-task-2 = (make-hash-table :test #'equal) + with map-width = (input-map-width map) + with map-height = (input-map-height map) + for antenna-locations being the hash-values of antennas + do (loop for location-1 in antenna-locations + do (setf (gethash location-1 antinodes-task-2) t) + do (loop for location-2 in antenna-locations + for antinode-task-1 = (antinode-location location-1 location-2) + when (and antinode-task-1 + (point-in-bounds-p antinode-task-1 map-width map-height)) + do (setf (gethash antinode-task-1 antinodes-task-1) t) + do (loop for antinode-task-2 in (antinode-locations location-1 + location-2 + map-width + map-height) + do (setf (gethash antinode-task-2 antinodes-task-2) t)))) + finally (return (values (hash-table-count antinodes-task-1) + (hash-table-count antinodes-task-2))))) + +(defun day-8 (input) + (let* ((map (make-map input)) + (antennas (find-antennas map))) + (compute-antinodes map antennas))) diff --git a/t/day-8.lisp b/t/day-8.lisp @@ -0,0 +1,22 @@ +(defpackage #:aoc-test/day-8 + (:use #:cl #:lisp-unit2) + (:import-from #:aoc/day-8)) +(in-package #:aoc-test/day-8) + +(define-test test-day-8 + () + (multiple-value-bind (task-1 task-2) + (aoc:run-day 8 "............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............") + (assert= 14 task-1) + (assert= 34 task-2)))