From 3a5790f8c51a1e163942bf5ec1bdcdca61f2deb8 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Mon, 4 Dec 2023 01:45:44 +0100 Subject: [AoC2023] day03 lisp --- .gitignore | 1 + AoC2023/day03/input | 140 ++++++++++++++++++++++++++++++++++++++++++++++ AoC2023/day03/solver.lisp | 87 ++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 AoC2023/day03/input create mode 100644 AoC2023/day03/solver.lisp diff --git a/.gitignore b/.gitignore index 4648da8..5f00747 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /AoC2022/*/solver +*.fasl diff --git a/AoC2023/day03/input b/AoC2023/day03/input new file mode 100644 index 0000000..44251ef --- /dev/null +++ b/AoC2023/day03/input @@ -0,0 +1,140 @@ +................458...689.556..3............197......582........720.........................515..352..286.........670.741.....895.626....... +...910.743..........................13..........................*.............775...956........@.........*................971.-............. +....*......406.507.97..846..............968+.........253........730...574............#....308......*.....798..............*.......894....... +....555...............*......%...............980.+43..=..239..........*......495................638.111.........*490...124...*........576... +.............807......176.....820...=.........*.........@................144*...........324..............82..745............775............. +...509.../..../...85/......9.........574....744....586....../..796................763...............................759............#........ +.....*...541...................930%................*.....831............494...44........799.....................870*.......834.215.675...... +.....23..................391.........17.......500..329.................+.....*......................431.....................-...........19.. +........................#.............*.704+./..................................152............=61....................668..............*.... +........894....334.............766.817.........302.-367.663......670.....+..........628..............592..................652......130..885. +....228...........*...183..................169*...........*...............836..........*...230............45.....$....200....#.......&...... +.......*........920..*....&........................865........60...=..........901-.....766.*...815..........*....410...*...............227.. +.....#.623..........183...28.....460....861......./............*....151....................759...*......341..84......928....906..470........ +..148........636................*........&....376...387=......187............741.308.............413.......................*.......*........ +...............-.158........362..220.......+.........................$..553..*.....%...........*.........359=......373....706.518$..274..... +.....688..279........441......+........320..596.............../.#...989...*....%.....108....139.216...........-...*......................... +......*......*399.......*.506.........*....................282..344......610...462..%...............876.....287...230......$.......#633..... +...221...111.........235....*...%377.319.............503.......................................402...+...=...............224.............980 +............*.............992...........................*..........945.891.................312*........747...........-...................... +.........350.........................147....957.314.183.483...........*.....................................613#.....880.........$177....... +.....................146*409..........*.....................-................392......892.....981...246.437......790........................ +......440..@860...............205*421..791.................652...456..$........................-....*.......565...%......140....511......... +.......*.............762.........................513.....#........*...596..656*.....959............292.476..............*........*.......... +.....276.............&...167....108.819...............845.........950..........577.*..........533.........+...........323.......12.......... +.........................*.......*...*.......348...........570.........998..........308..........*.765.......................76......558.897 +............530....992....512...560...755......*..569.........#...................%.....461...896.....&...813......763..............*....... +424..........+....*.....*.....@...............922.....................445.99......88.......................=...803....*..14.440....139..349. +...*............155..417.911.383.........434.......+...345.763..279...#.....*6...................................&.814....*..#.............. +...147.............................762...+........856..*....*....#..#............82..%.........=.......*................619................. +.......#668..........88.....................-..........448.710......25.427........&.740....*...44.608.809........%..................119..... +..................*........&....../........86.....724.....................*...924..........240....$.......935..945.....545.641.......@...... +.....578.......565.749...342....338..................*786...305.....315..627.+........924.............+..................................... +........*135.................@......786.........911........*......*..............193.@...........862..337.......879.......992..681.598...207 +..........................646..........*105.......*......682...683.53.849..750...*.......+......*...............*...787*..........*......... +............899......230.....................792...779....................@......918..955...731..233...........496..................109..... +...905.........*.......*....../...550*675...*..........652..........$581.......................*..........................$.....204...*..... +....*...146.171........162.944.............613...34.........563.321......648.................278.....69............519.....558.+.....573.... +..134....*.........260............815*20..............286....*.....*....*.....100...261.....................=850....*....................... +......872.........+.....................................*..971..669....733...$.........*..........610............983..801=..........153..... +601.........484@.........109..........317..............424............................298.........*...976.....*............................. +...*....................*............*....316.201...........935*................................22...&.......137...336.....557........4..... +..745.........286.....989..........582.......*..................277...795..........335.................................520*............*.... +.............=....&.............85..................=...19...................281+..@.............940.+970....318.................281..216... +................361................178../.........725.......*.........548..................*190...*..........$.....%.....&..........*....... +..57*......131*.........436*375.....#...168..............305.435........*...........674..........245....$.......$...404.270......812..707... +.....440.......318.525.........................*.........................540....799...*................344...432.......................*.... +............$......*....526...730..142......927.785............................&.....509.......45................849*.......276......993.... +..362*541...187....393........../...........................365...........442.................*....105....*..........256.................... +........................568.......&.98.........+.=427./718..*......500#......#............692.547.&....753........=......................... +.........652@...........*.......836.&........333...........144.................*...33....#..................593.540.....968..822.....450.... +..............518....442............................521.............884.303.85.483................594.......*..........@......*.......*..... +.......278......*........674..............844...435*.................*..-...&.............837......*.....333..#849..........607.......177... +.........&.......302........*........*519....*...............270....174....................*......579................66#..........425....... +..............*......*......205...833.........672...............*........269.440...771.....446.................=.*...............*.......... +.134.527.....14...910.845.....................................113..-....*......*..*..........................579.171.........287..828....... +....*.....................726.942..83..........264.752..............188.765..359...182................380..............-.....*...........734 +..........................*........#.............$.........322..............................*...........$....@830.....989.246...&........... +.....*260..452.325.................................................246...................466.915..............................-..417....&558 +..961........%...........#....203*690..659..@.....424*..............*.............*.................797.363...$813...........340............ +........................260................608........565.......@....856.......576.159.....691..23.....*................*562.........373.... +......235.962..311-................*....#......218...........129............@..............@......*354................33................*... +649..*....&..........140......910..613.544..........420.............903*....825...............................139.819............595........ +.....349......318.....$..746...*.............264......$..&...607........879................................+.....*...............&.......... +...........$......259....*......609.............*.......619.....*.............#...68.45*...560*723..........171....12=....277.........563... +...39...428.......*.......551...................962...=..........620........776....*.......................................*..626.......*... +...&..............370.#.......*807..103...820.........842...562*.................646..48....%..412...........684*754....143..*.............. +............866.......77...225.............&......152...........241.........628=........*.462....%.926........................990..439...... +.352.............................&615........633+...*............................996..388..........*...516..............107%..........*743.. +....*...............970.......%...................564........................99..../........*790..239.*......94....688...................... +.211.....342+.........*....521...............788...............+........839............+.265...........486................637......+...@.... +.....................82..............187...#....*731..230.......50...................480.......8.................................507....660. +...103..762*436.523*.......................250.........*...#...................................*...518.916...........................$...... +...*................439....553....................479.466.553............699#..3.$...........695.......*....................=.......923.714. +..867.+971....45...........*..........669.....885................605.272......#...175...836...........15...................257..144......... +............................579.......#.............................*.....877..........*.......330........141=.............................. +.................................881......98..400.517....................*....453......509........*..................................879.... +....99.........300.882.....670....*............*...................712.119...*................473..290...630..........510......@.....*...... +.../.....979...../....*959..+.....189........123...........402......&......394....329...........%.........*.......208*.......583...785...113 +...................&............*........402......369..521........................*........314.............7.............564................ +...............555.430.......630.854........%..........+......676...........980+.........................................*...............849 +...........531..=.......................*......139............-..........................5..969=....338.........464.......508.......843..... +.............*.......118.............999.915.....*.&.....................755.........501*.............-....2.........766........3......*792. +...............@706..&...258.#501.............442...469..........332.327.*....@..............................*..........*...661..*.......... +...955.....900......................#.....986..............705%.*.....-..884..290...177*531........632......802...936..39.....$...952....... +..........@...........483........553..544..#......920............340.............................../.................*.................396.. +...149................*..............#................446....405.......555...........................98....313........834.#.....%...../..... +....*...409...266...29.......283..............=................*........*............%830....748....*......*..............457...592......... +...310.*......./....................*......138...197..........169...308.387..923............*........598....50...980........................ +........178.............236......865.134...........*.436..772...................%..688.&.....850.66.........................796*897....679.. +................170.......=.784..........$463...871..*.....*...338.........16........*.876.......*....................368*..........#...*... +...........270.....*682.......+.....................129..802....*...505......*822.834.........575.....309....@.577........6......926..632... +.....448.....@...*................105..960.480..................293.*....................598......$...*....686....*..759....13.............. +.........535...507.........@.........@....*............390..........42.........919....-........928..584.............%........*.............. +....12#.................661.....535.............397........................*.....*.....788...................936....................314..... +................................./.........417......528..492.......202..654.143.476.73.....796........282....*........317..............-.... +.....511....................396.............*.............#.........................%.............&........861........*....919..954......... +......*............704*853........396.772...407.....499......$.....393.....-317.648..........262.72..............482.418.....-.......661*466 +...387...300...................+....=../........688*........895.2...*................/........*.......25.......................268.......... +............+................253.........16.......................665.......936..451.222..299.498.....*......*669.....652........*.218...... +.................49....43..........567...*............%...226.........188.....*...$.........*......510....717.....$53..*......474....*...... +...........#..........................#...906...206..479...#.............*.599.............598.........................904..&.....808....... +.........876...............295..................-.................998...34.........=348........499.81...........@..........681.............. +....452........407.350.......*..@971................721............*....................129..-...#.............508....744................... +.......*17.....*.......739.136........................%.........635.........310.487....*.....746.....964.....+...........*.....639.......... +...........314..471.............................768.................640.......=.../..387.............*....420.............975...-..230...533 +..............*.......389.........&.372........%.......537.....799.....*.................678....661...531......460..................-....... +............97.........=....180.943...*...=.......................*....424..................#.....*...........*..............$.............. +...803..............................529....264...............$...810..........810..................183......429...730*52.709..896..696*481.. +......*82.570-............$.......%...................754...528..........+399....*893.....176*............................%................. +........................731.......679...........110*.....*........749.........................325.803*.....130..................+.820*541... +......739*.....707..........@577......4....210......21.396..........*..........822.930................689......250.749.774...539............ +..........113.........................*......-..477.........112.....881......$....*.....$....$...........................*.................. +.....+...............................809..........*........................459.......496...354....979...231....31.....-..541...168...708.... +.....954..........642...267........*.....453......812...993............................../.......*........*..........267..........-....@.... +.............9.........$.........307......*...364.........*..266.757/...800......805*197.453...758.617.....390................329........... +..............*954...................*...358..*..........859...*........*....848...................*.....-...............+....*...43.671.... +.........107........596...*.......941........555.....378......824.348...207.....*....469/..352*..........474...........961.848...*......*... +............*......*....659..............+86........*.............%...........898..............307...............824.............735...584.. +....540....734...463...........374..............204.........633*......................................627.......%.......%...%............... +....*................727..606............752.......+............522........@.............187&...$..89.+................311.384.............. +.........368*901...................120........308......783..................361................901..........770.163.............348...278... +.........................10..874....*...................*................................826.......*440.=.....#.*...631...........*......... +..........145...........*.......*..665..567....&.........486.........%869......679........*.....369......195....890.*...4........100........ +......664....@...314.....49....859.....%.......8.............971..........905.....&......595........................742...............340... +........*................................852........120.......*....80....*...................823.....%.........476.........893.............. +........748.178.50*479........863....=.$..............*...........*....808...495...305......*.....684...47............./......*634.......... +.....#........*.........&.....$.....83..674.571....575...........756...............*....*....546........-.....*124...199.758................ +...346..937..70.........569..................@.........12....=...............-...742....694................405...........*.......$.......... +...................................617...........958....*.778........540+.293.............................................502.834.....537... +...#.......47................908....*..............*.622.......................980.390#......785.104....................-................... +....261....*.......756..............555...%......893...............810..........-........804...*...$.942........739..773.................... +.........390...765.......655......&......497.........885.....2.....$.................89../....636......*.862.......%................717..... +.......2........*...897....@....354..+...........959...*....@......................@........*.......576..@.............698.................. +...............307..../.$.............100..........*...324....*.821....377..........939..134.625.......................*.................... +.........................556.....................63........307...$.....*......=.....................462.....427.........822............681.. +....................+........................................................864..........8.....550*............665...........#...956..*.... +.780.......864.......435......811....760.................452.........................................&..886......*...........792..@.....245. +....*.........../............*..............661%.213........=.........310/.726.....739...791...387.123............924...............281..... +..139..........45......#...316....=................*..........678*..........*..@10..*......*..*.........*...61-......................*...... +.....................815.........786................186...........640......388.......415....4..36.....894.........................303....... diff --git a/AoC2023/day03/solver.lisp b/AoC2023/day03/solver.lisp new file mode 100644 index 0000000..6f9c799 --- /dev/null +++ b/AoC2023/day03/solver.lisp @@ -0,0 +1,87 @@ +(ql:quickload '(fiveam cl-ppcre)) + +(defparameter eg-input "467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598..") + + +(defun inbounds (row-max col-max) + (lambda (cell) + (destructuring-bind (row . col) cell + (and (<= 0 row row-max) + (<= 0 col col-max))))) + +(defun contour (row start end) + (list* + (cons row (1- start)) + (cons row end) + (loop for i from (1- start) to end + collect (cons (1- row) i) + collect (cons (1+ row) i)))) + +(defun is-relevant (limits rows) + (lambda(row start end) + (some + (lambda (cell) + (and (funcall limits cell) + (destructuring-bind (row . col) cell + (let ((char (aref (aref rows row) col))) + (not (or (digit-char-p char) + (eq #\. char))))))) + (contour row start end)))) + + +(defun solver1 (lines) + (let* ((rows (make-array (length lines) :initial-contents lines + :adjustable t)) + (limits (inbounds (1- (length lines)) + (1- (length (car lines))))) + (keep (is-relevant limits rows)) + (sum 0)) + (loop for row below (length lines) + for content = (aref rows row) do + (cl-ppcre:do-scans (start end _rs _re "\\d+" content) + (when (funcall keep row start end) + ;; (format t "~a ~a ~a~%" start end (subseq content start end)) + (incf sum (parse-integer (subseq content start end)))))) + sum)) + + +(defun adjacent-numbers (line gear-start) + (loop for (num-start num-end) on (cl-ppcre:all-matches "\\d+" line) by #'cddr + when (<= (1- num-start) gear-start num-end) + collect (parse-integer (subseq line num-start num-end)))) + + +(defun solver2 (lines) + (let* ((rows (make-array (length lines) :initial-contents lines + :adjustable t)) + (limits (inbounds (1- (length lines)) + (1- (length (car lines))))) + (sum 0)) + (loop for row below (length lines) + for content = (aref rows row) do + (loop for (gear-start end) on (cl-ppcre:all-matches "\\*" content) by #'cddr do + (let ((ratios + (append + (when (funcall limits (cons (1- row) gear-start)) + (adjacent-numbers (aref rows (1- row)) gear-start)) + (adjacent-numbers content gear-start) + (when (funcall limits (cons (1+ row) gear-start)) + (adjacent-numbers (aref rows (1+ row)) gear-start))))) + (when (= 2 (length ratios)) + (incf sum (* (car ratios) (cadr ratios))))))) + sum)) + +(fiveam:test solutions + (fiveam:is (= 4361 (solver1 (uiop:split-string eg-input :separator '(#\Newline))))) + (fiveam:is (= 467835 (solver2 (uiop:split-string eg-input :separator '(#\Newline))))) + (fiveam:is (= 531561 (solver1 (uiop:read-file-lines "input")))) + (fiveam:is (= 83279367(solver2 (uiop:read-file-lines "input"))))) -- cgit v1.2.3