advent-of-code-2023

My solutions to AoC 2023
git clone git://git.entf.net/advent-of-code-2023
Log | Files | Refs

commit 6350c14d1a86766b62b99178a04dc829df7fee9b
parent 99af642db393d53c811ac5bcae0016c052dd3287
Author: Lukas Henkel <lh@entf.net>
Date:   Sun,  3 Dec 2023 06:42:41 +0100

Day 3 task 1

Diffstat:
Ainput/3.txt | 140+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day-3.lisp | 34++++++++++++++++++++++++++++++++++
Msrc/utils.lisp | 16+++++++++++++++-
At/day-3.lisp | 17+++++++++++++++++
4 files changed, 206 insertions(+), 1 deletion(-)

diff --git a/input/3.txt b/input/3.txt @@ -0,0 +1,140 @@ +416.........................559...............417...............785.......900.......284...........503...796....992.......................... +.........702*....772............378..569.........&.49..606...14*..............$.453*.........307....*......$.....-.................995...... +.....................458...856......+.........+....&..............680.......104.............%....516.................................*...... +...........822..174..*.....&...........711.746.......&............$....../.............656....#...........265=......634.*.............430... +..827.137..*...*....39................*..............856..............767........522......$..773....619..............*...287....501......... +..........726...511.............*.....320........476...............................*................%...899....72..731...........%....$..... +.....861..............232....223.933...............*.@........424*618.858.......................................$.......338.205........535.. +.......#.............-....................676...713...427.................-.......615.........126...................=..*.......*...&........ +....40..........996..............520.974.*..........#......*.566........907......................&...214...996*911.115.363..960..897........ +....+.............*....................$..172.....559...763.....*............554......*.............*....................................... +.......527...#.....90.+....66.................................890..............=...802.93.131..791...209......&........928......303....$.... +950.....*....773......105..............725........................................................%............886.......*........$.384..... +........383......741..............@..=....#.....179.18..%974..........624.......64.266.................701...........%..671.721.........942. +..................*.............914..548..........+.*.................*...........*...................*.........834.394.....*............... +.......502*80..960........................25........464.........831.846........25.........329..985...458.+.....&................377..659.... +..........................................*.....292...............*............/..................@......350........938............*....$... +...738..............428......+.......311...742.........236*631....816.&......+....86.........81.......................*.973*341.266......... +......*.....673......*......614.........*.......689.48.................450...816.....754...........258..@585.......154..............@....... +...231.....#.........681...........855...775..........*.....=..257...................................*.............................469..=682 +........#...................807...&.............418.19.....259....*459..906...185.356.......778.......230....................556............ +.......924.........459......*..............804.=.........................*...*...........&....*................=........836..+......618..... +.....=............#.......900........+...........702..383........%..*...250..503.......637.808.......97/....@...370.......&........#.....710 +..988........685............../663.273...........*...@..........16.251...........$..............*..........964.......658.....537........*... +...............*......171*......................714.....543............737.....372.............941.............113..*....=.......853....733. +............470..161......508.....56...170.............$...................389.....544.....208.....98.........%.....617...884......-..@..... +.766.591............../.........-......*.......210.........618......*874....-.........#...*....129.=..194......../...................992.... +....../....170@..140+..753....918....467.854....*................989..........979...............*.............-.907...276...931*618......... +..700............................................801....929..859.....#..........*.............594.493......981..........*................... +....+............173..............................................251........629..........970........*................136.........388...*... +......721.......$......911....766..................541.234....=...........47......614....@.....*217..885.680.742....................#..84... +.377*....*81.............*.......&...937...........+...*....155..@......*............*......648............&...*.....&401......493.......... +.....1.................533............*..399...........861......951..709.331..126.....876.....................289.........@.....*.......*... +..........107....452-......594.888.431....*..+660...........79................................$......275*876.......978.....594..400...39.... +.........+................*....*.........925..........$.......*........599.......228..........60....................+....#.................. +...283...................177...708...........642....661........294........*............................288*793.867.....482.479.769....73.... +...........617..855*...&.............605................783.91.........847................&...../...............*..#..........*.......+..... +.672..303+...*........967...............*453.....884@......*...208.949........*..........986.....249.....352..818.596.56.................... +...*..........88...............569.....................975.....*.....*..638....619.......................+.............*.......*.......462.. +....500.....=...................*.................*205..*..-...228.508....&........................395..............845.....683.365....*.... +........197.672.841..............214.=...847...912.....24.349...................%....584.257............152................................. +.626....-..........*..447............714....&.....................463....287...360........*............*...............*.................717 +..........53......334...*........................*167......................*.......................554..246...........295.396......./....... +.......24..............92...........897.......220...........................930..703...414.....711..*.........723.........*....117...146.... +.......&...........548.....324.21......................622.........820.404...............*....../..139......&......*.......578...*.......... +.....*......204......*..........*.............226...........663.......*......555.992.....502...........631..49.#....366........677.......... +..958.350.....*......44...694...449..-39........*...........-...786.........*......*............................140......................... +...............477.........*...................815..............*..........815.................521....................&...273........103.... +.........828............781....464..................276......646....665-............924..........*.......955........759.........934...#..... +........*..........*..........*..........15.193.......*.....................&.................808....347*......963........*....+............ +.........810.....360........638...434.....*....*751....812.....573...........85.........+.........95.......892...*...153...220..........187. +....714*....................................................80*....162=.................214...........842...&...39.......................... +........265.........................51.......285$.......................586.......=.................................*.....*..............948 +..989..........22.=......374.......%....................142.......736..@.....507.636.797.....273.........872.....567.978..334.....382...*... +..........@.......68..=......................116.130.......%.250../......-...=........*.........*486.415..........................*....813.. +.......505............61..140..........435..........*691......*........852..........571....408..............12......80.......228...109...... +.........................*.........207...*..24................402.=...........................@....................../...162................ +....285.................672.552........492....$./..................3..620.391...............................179............*................ +...*..........159...428.......*.................390.111.........$.....*...*..................348.355.481.......&.210..99...41............... +...52......12..@..........808.810.897...................663..280....57..............476.............*............*.....*........208=........ +..........*...............*.........@.286..................*...........................*..........................905.296..............148.. +.......298......119.....172.................622$..637................#......342.......679.111......-968..104*478............#.......*....... +.../.............*........../......................*..............658..........*718..........*136.....................503.899....889.498.... +....691........341.262..36.549...........386........437.............................662...........848............#......*................... +.......................*..........936...*...............................-...........*......516....%......358....707..535...........841...... +......$..............639............*..798.../..67%............137...716.......313.247......................-............@.....371.......... +....433.677..605.267................1.......930........478........*........565*................................869.......372..@....228...... +...........@..*..$...794.........................74.......#./......833.348.......................................+................*......... +......865...............*.........................*..........839.....................=......................................916..84.@....... +.................-....451........541@......468..684....18............759.............499................124.....426*.........*......882..... +...68*...........614........509...............*.........&...956*308.%........&....36...........480+....../..........917....32....#.......... +......363..377.............*..........441.....418..........................279.....*.................139..........................944....... +........................412............*..........%....920*585....526*............931.346&.807..840.....*.626-...#....................923... +.....283.....924...+..................628.......33....................908..766..............*........336..........446...........191......... +...............*.249........@264..35...................502.791.#...........*......=............126....................957....71....*.768.... +.........144&.36.....216.........*...........................&.730..........201..581.704.........$......715.............=......*.......%.... +...349..............*....598...949.........189....981.....#.............524...................*............=................440...847....... +.................967....#..........999*6..%.........-......604............&.189.626...#774.159.647....................168.........../...329. +..............................................481.....*........................*.................................747..%.................#... +..........245...878.......495....57....841.........351.517..........-297........................343.599/........&.......360..........-...... +430........&..............&.....*...&.....*885...........................9....392.......93..336*...........................*321.....86...... +....469......#.................999..796..................899.........250*........$.......*........247............................+.......328 +......*...696...615..300..603..............................*....232...................402...501......*.......77.271.....@.........146....... +......170......%....$.........*..991..........782.49.......128.............................*........161........*.....648......123........... +..........646................901..*.............*......134...........517..2..287........513...............................644...*........... +...................999*620.........541.379.....488..18*.......................*...............208....931.338..%143...........*...19......... +...+.......938...........................*...%.............&........626......268.418......841*..........*...............820.395..........913 +.723...834*..........+............162.667.....84............803.804...*............*................399........172..530*.................... +.....................596............*............................./.287............852....623....@.*..............@........168...965..*17... +.....=488...................758....607.....&......53%......................105.683......-...*.860..244.....&....$....232.................... +.............................*..........333....................197........+....*......334.954.............686...464...............544....... +.139=..............754...993.677..../.......933..........391....*....357*.....952......................@..........................*......... +..........831............../.........94.600................+.121.........183........18..686.....665*..823...........575...........68........ +141...800....*....692...........254@.....*...819-.....................................*..@...................17.......*...815...........686. +........%.602........%.................186..........562.93....774.....................11....948....912.........*328..243.*.................. +.......................................................*..........607....280..................&....+.....................548.&897.100@...955 +......504.....605...........889@.............975..664....$.....#............*....141.................378%.........812....................... +.....=...........*.....730...............%...............899..480.....*....375.....-.444.........47*.........@63...........828.....468..462. +.......607.../...23....=......=........383.............................522............*.............433..360..................*372...*...... +........*...975.............428...................592*......@258...114......%......431...................*......+........658.......560.163.. +........16........................329.......315.......973............*...333....@.......$....782..18...58....533........*...............*... +..327.............563...889.......*...........*..595...............913........671..354..937...%..*...&...........&.787...975.......676...247 +.....+..&...401..*..............713........=.757...#.873*47...#465..................................440..588/..859.%..................*..... +.665....133...%..196....907..............348..................................162........305...................................163.-........ +....*....................*...........170.........222.804....784..............*...................................................=.742...... +..239..338.....27-.437..543.........*....609......*..*......-...............582..-...........343*560....852.../....636..263................. +..........*900.......*............17...........948.....597.....=....922...........682./255................@..35.......*.......211-..#....... +.......*...........686........984..........78......896*......13........+....................985...&....................278.........876.835.. +....630.82....$851.....905.......*..281...*...............................352..........*746........215.877....845..........471.852.....*.... +.........................*.....757.*......625...........924......878..........912...............-.........*../.....538%......+.........871.. +..954*712..977.....-..762..........236.........527*674.*.........*........811*.....228....&.....5........739...483.......................... +..........*.......470..................................707....363.................*......525.......303..........*........463....537......... +...460...947...............130.....757./96........*.................=...529....+..466........527................65..%648....=.....*......... +.....*......................$....../..............968..............479.&....630............................598.................952.......... +.......&..........793...................................514...............-...................311..........=....83......#.............124... +.975....399.......*...-.......*814...663..................*....*...........381../...............*.............@.........284............*.... +...*............367....198..........@.....992.....716*529....96.................729.329.688...%.322.-......67.79......-.......335....997.... +488............................632........*...../......................387.............*....225......491.............391..750...%........... +.......861.527......778.165.......*........187..916.......845....-....*.......873.................................@......................... +..545.....*.....$.....*..*......627............................736.376..845...*....594........+.......171.........292...........*750..-..... +.................407.403............+...342.................+..........*.......897..........400.......$......................910.......350.. +...........................40..284.66...*......494..........255.......653..............866.................942=..................*52........ +....&........827.......296...*........$.401......*..............892*...................$.............1.164.......343..........485........... +.930............*.........*..944...308...........763....../.........62.....113....=...........421........*..........................580+.... +........50*.....934......705............................999..................*....541...............847-.950..............*131.............. +...........209....................................&404...............276..242.............723.652................873...313..............24.. +......*........606....550#.....2./........@..................899.......*.....................*..........235..112...........704.....337...... +.......837....*...............*...83...993.....*416.........-....580%..535......../.....-204...............#..*.....93+....*...723./........ +..............168.....753..593..............504......./........................488..............=.............458.......872...*............. +...#85...................&......911/...................880........315..872..........=....494..349....466..428..................40........... +........939........648......................*................227...*......*.......924...*...........*.....*................................. +918*......&.@........*...902..269..834....87.826.........../...*...919.118...817......109..........933..643...........&..............-...... +....949......883...111..%......*...%..................710.464.943.........../...................................842....305.....469..289..... +..........................%...974......*407..168.647...*..............617.......498/.....848...........@99........*...........+............. +..........176...........120.........469........&...#....997......464.......274.............*......./............477.417../.......738........ +.........*.......964.........291..........................................-.............2..29...272..465...............-..819........718*265 +....298...747.......#.......*.....+745...........460...741*762.275...491.......+.735*34.*............-....*841......+............*.......... +...*........................593..............298...#............%....$......225.........943........................23.....702.601.616....... +..889................695........654..750.....*.............637........./...............................780....*726....233...*............... +..................../.................*.....453.....642....*.........828......@...94...........152/...*....790.......*.....445......../..... +...........................51.......681........................271..........719.......................964......399..426...............456... diff --git a/src/day-3.lisp b/src/day-3.lisp @@ -0,0 +1,34 @@ +(defpackage #:aoc/day-3 + (:use #:cl #:aoc/utils) + (:export #:day-3)) +(in-package #:aoc/day-3) + +(defun schematic-symbol-p (symbol) + (and (char/= symbol #\.) + (not (digit-char-p symbol)))) + +(defun extract-part-number (map y start-x) + (loop for x from start-x below (array-dimension map 1) + for char = (aref map y x) + while (digit-char-p char) + collect char into chars + finally (return (values (parse-integer (coerce chars 'string)) + (1- x))))) + +(defun day-3 (input) + (loop with map = (make-map input) + for y from 0 below (array-dimension map 0) + sum (loop with number-start = nil + for x from 0 below (array-dimension map 1) + for digit? = (digit-char-p (aref map y x)) + when (and digit? (null number-start)) + do (setf number-start x) + unless digit? + do (setf number-start nil) + when (and digit? + (member-if #'schematic-symbol-p + (map-neighbours map y x))) + sum (multiple-value-bind (number next) + (extract-part-number map y number-start) + (setf x next) + number)))) diff --git a/src/utils.lisp b/src/utils.lisp @@ -6,7 +6,8 @@ #:read-input-fields #:read-input-match #:char-number - #:make-map)) + #:make-map + #:map-neighbours)) (in-package #:aoc/utils) (defun normalize-type (type) @@ -85,3 +86,16 @@ (push fields data)) finally (return (make-array (list height width) :initial-contents (nreverse data))))) + +(defun map-neighbours (map cy cx) + (loop with height = (array-dimension map 0) + with width = (array-dimension map 1) + for y from (1- cy) to (1+ cy) + nconc (loop for x from (1- cx) to (1+ cx) + when (and (not (and (= x cx) + (= y cy))) + (>= x 0) + (>= y 0) + (< x width) + (< y height)) + collect (aref map y x)))) diff --git a/t/day-3.lisp b/t/day-3.lisp @@ -0,0 +1,17 @@ +(defpackage #:aoc-test/day-3 + (:use #:cl #:lisp-unit2)) +(in-package #:aoc-test/day-3) + +(define-test test-day-3 + () + (assert= 4361 + (aoc:run-day 3 "467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598..")))