advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit a5f5b0fbea55aad004dbbea0b471859648c1f129
parent 8c7f875fd68b325cb9fa45f0530dbd0d5f8e4636
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Sun,  3 Dec 2023 17:32:02 -0500

Add 2023 day 03

Diffstat:
A2023/day03/day03.scm | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2023/day03/input.txt | 140+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 261 insertions(+), 0 deletions(-)
diff --git a/2023/day03/day03.scm b/2023/day03/day03.scm
@@ -0,0 +1,121 @@
+(use-modules (ice-9 popen))
+(use-modules (ice-9 rdelim))
+(use-modules (ice-9 format))
+(use-modules (ice-9 hash-table))
+(use-modules (srfi srfi-1))
+
+(define (is-valid-loc schematic loc)
+  (let ((x (car loc))
+        (y (car (cdr loc)))
+        (max-x (car (array-dimensions schematic)))
+        (max-y (car (cdr (array-dimensions schematic)))))
+    (and (and (>= x 0) (< x max-x))
+         (and (>= y 0) (< y max-y)))))
+
+(define (is-adjacent-to-symbol schematic x y)
+  (let ((found #f))
+    (do ((dx -1 (1+ dx))) ((or (> dx 1) found))
+      (do ((dy -1 (1+ dy))) ((or (> dy 1) found))
+        (if (is-valid-loc schematic (list (+ x dx) (+ y dy)))
+            (let ((cc (array-ref schematic (+ x dx) (+ y dy))))
+              (if (not (or (eq? cc #\.) (char-numeric? cc)))
+                  (set! found #t))))))
+    found))
+
+(define (find-part-number schematic)
+  (let ((part-numbers '())
+        (curr-has-num #f)
+        (curr-num 0)
+        (curr-num-adjacent-to-symbol #f)
+        (max-x (car (array-dimensions schematic)))
+        (max-y (car (cdr (array-dimensions schematic)))))
+    (do ((x 0 (1+ x))) ((>= x max-x))
+      (do ((y 0 (1+ y))) ((>= y max-y))
+        (let* ((c (array-ref schematic x y))
+               (in-num (char-numeric? c)))
+          (if in-num
+              (let ((cc (- (char->integer c) (char->integer #\0))))
+                (if (not curr-has-num) (set! curr-has-num #t))
+                (set! curr-num (+ (* curr-num 10) cc))
+                (if (not curr-num-adjacent-to-symbol)
+                    (set! curr-num-adjacent-to-symbol
+                          (is-adjacent-to-symbol schematic x y)))))
+          (if (or (not in-num) (= (1+ y) max-y))
+              (if curr-has-num
+                  (begin (if curr-num-adjacent-to-symbol
+                             (set! part-numbers (cons curr-num part-numbers)))
+                         (set! curr-has-num #f)
+                         (set! curr-num 0)
+                         (set! curr-num-adjacent-to-symbol #f)))))))
+    (reverse part-numbers)))
+
+(let ((file (open-input-file "input.txt")) (schematic '()) (ans 0))
+  (while #t
+         (let ((line (car (%read-line file))))
+           (if (eof-object? line)
+               (break)
+               (set! schematic (cons (string->list line) schematic)))))
+  (set! schematic (list->array '(0 0) (reverse schematic)))
+  (set! ans (fold + 0 (find-part-number schematic)))
+  ;; 514969
+  (format #t "Part 1: ~d" ans)
+  (newline))
+
+(define (find-adjacent-protogear schematic x y)
+  (let ((protogears '()))
+    (do ((dx -1 (1+ dx))) ((> dx 1))
+      (do ((dy -1 (1+ dy))) ((> dy 1))
+        (if (is-valid-loc schematic (list (+ x dx) (+ y dy)))
+            (let ((cc (array-ref schematic (+ x dx) (+ y dy))))
+              (if (eq? cc #\*)
+                  (set! protogears (cons (list (+ x dx) (+ y dy)) protogears)))))))
+    protogears))
+
+(define (find-gear-ratio schematic)
+  (let ((part-numbers '())
+        (curr-has-num #f)
+        (curr-num 0)
+        (curr-protogear (make-hash-table))
+        (protogear-specs (make-hash-table))
+        (max-x (car (array-dimensions schematic)))
+        (max-y (car (cdr (array-dimensions schematic)))))
+    (do ((x 0 (1+ x))) ((>= x max-x))
+      (do ((y 0 (1+ y))) ((>= y max-y))
+        (let* ((c (array-ref schematic x y))
+               (in-num (char-numeric? c)))
+          (if in-num
+              (let* ((cc (- (char->integer c) (char->integer #\0))))
+                (if (not curr-has-num) (set! curr-has-num #t))
+                (set! curr-num (+ (* curr-num 10) cc))
+                (fold (lambda (loc res) (hash-set! res loc #t))
+                      curr-protogear
+                      (find-adjacent-protogear schematic x y))))
+          (if (or (not in-num) (= (1+ y) max-y))
+              (if curr-has-num
+                  (begin (hash-for-each
+                          (lambda (k v)
+                            (if (hash-ref protogear-specs k)
+                                (hash-set! protogear-specs k (cons curr-num (hash-ref protogear-specs k)))
+                                (hash-set! protogear-specs k (list curr-num))))
+                          curr-protogear)
+                         (hash-clear! curr-protogear)
+                         (set! curr-has-num #f)
+                         (set! curr-num 0)))))))
+    (hash-map->list
+     (lambda (k v)
+       (if (= (length v) 2)
+           (fold * 1 v)
+           0))
+     protogear-specs)))
+
+(let ((file (open-input-file "input.txt")) (schematic '()) (ans 0))
+  (while #t
+         (let ((line (car (%read-line file))))
+           (if (eof-object? line)
+               (break)
+               (set! schematic (cons (string->list line) schematic)))))
+  (set! schematic (list->array '(0 0) (reverse schematic)))
+  (set! ans (fold + 0 (find-gear-ratio schematic)))
+  ;; 78915902
+  (format #t "Part 2: ~d" ans)
+  (newline))
diff --git a/2023/day03/input.txt b/2023/day03/input.txt
@@ -0,0 +1,140 @@
+...317..........214.....................................751.................................630...479..205....41.993............416.........
+...*....813........%....572........%...629.154......518....*....365..................-.......*.......#.....................422...........661
+269.......*...58...........=......264.....*..........*......937.-...........235...303.........848..............195.....154*.........144.-...
+........476..@...162.855................$....288...821..............107.....-...........290......../..301.........=...........135..*........
+618.............=....*...547...........441....*........................@................*........851....+...................+..*...102.310..
+...*961....905......534..$...377...............854...341.342....485.......109....89.995..927..............................90..997......*....
+............*...................*.................../.....*......@...........%....*...*.......................+44.....913.........9.....891.
+....62......724...592....946.878...854....$.701............480......575.246....119.....54..296.906..684..................*.........*838.....
+.................+......*...............478....................469.....*..................*...........+..502..156.........821...........983.
+....................315.764..996..571...............=........................804.......&...892....958.....*......*.............61...........
+........+...697*.....+.........*...*...........@.448........................*.......322.............*....309..37.602.....775....*...........
+32.480..665.....557......+...435..449..691..327....................#....921..535..........755...43...597........................322..7......
+......*.....848.........745.............*.......679/............106.......&........404......+....*...................888..750........*......
+......93...*....=.............*297......550...........324.597............................................819.....997*......-...734$..588....
+.........363..112..754...............&..........723....................14..954.............175....773...*.............&.....................
+..................*.......#..........502......../...............606.........@..778*.......#.........*....844...713...743....-.........&.....
+............827.895.-539...469...938...../.248.....................*.49............749.............783.........$...+.........33....471......
+..............*.................../...517...&......368*710......432...*............................................841.=.................194
+.....*491....395.......&481.................../.#...................83................668..181.....698..155*...........981.200..............
+...................219.................196.473..224.....=.....733*.......=.........98*.......*.420..........671.....*......*......369.205...
+........@......696*....669.....50.....................853.593.........168....@.............802..*...388............3......792........*......
+......641............./....62..*...836#.......*169.........@...............872.................416....*.................-...................
+..........................#...377..........179.......101........+504.................*554.............947..............447.............830..
+243........*902.....#................480*...........+.....*258.........*243.......389.......&....................723.........838...991......
+........182.........33..........840......616.733.......756..........290.....................145..119..........................+......&......
+..............................................*....807...................777......+..556............@....85#................................
+.212.664..3......316..579..462..............435.......*............@....*........36...$........572................703..732........@.........
+...........=....@.....*.....*............*.......-..337......656...411.357.373.....................*21....37.........%./.....918...185.387..
+...350.................338...161.856..157.906...527...........*............+.........*336.......204............755.............*............
+....&...........683...................................801......313............831.230......374.......................383.....940.48.........
+...............*........447......635....335......%.........627........44@..........................895....................@.................
+....$....958..943.......#...935.%..........*...794......16.*..................+...........100..469........743.*.........529....=.......883..
+..308.....*........*776.......*.....*....430............*..562.......895......765.................*.........*..776..........246..921&.......
+.........200...............*..1....572................814...........$......#..........138...&....487..668..58.......485.....................
+...*845.............935...958.................................667.....%.121.......691.......245.........*..............*........131.........
+.........*............*.............=............................*.593.............*..................380........189...219.211......@.......
+.....152..783........634...........521.+.......26.............659...............328...%.............................*........$......397.*...
+......*.......124..........299..........808.....*........359................424.....%..454..........553.............49..562.....257......723
+......641.........24.............576.............975........=..............*.......974.............*...........146......*......../...@......
+940.........&....@....221........*.........149.......&...............689....418...........538...................*.....985............526....
+.............192.........*816...582...561..+..........915..981%.......*..................*......&8....+.........497..........145............
+.........144........$....................@......................425.933....846*824.$...370.342*....375...641..=........927.....*.......160..
+............$.......925...67.655....57.............=905..881....@..................585.........746.............224..........224...864...*...
+.....872................#..*...$.....*.........#............*...........979....699...................947.........................*.....788..
+............949......676..42........340.....694...144......712..296..45...*.......*..............191.@.......889-......=786.....417.........
+...........*.................846....................*..160.......*.......403.....347...914.91...=...........................................
+.371.=....904.643......816...*...647...$.858.......431..........486..........706......*....-..............961........................698....
+......398.....*....252*....591..../..224...$..341...........#................*................956............*...27..............57.....*534
+.............363...............................&.............69........163.233.580....113*367..............44...*..................*406.....
+...893..457..........91%........................................538.....$......*..............................475....864.......289..........
+..........-........................549............909..572.....-..............385.............97.....................=.....159..*...........
+...174...............................*...576.875....*.....=...................................*....506..115...............*....856....408...
+.....=.......685....58........177.965...-.....*....489......402/..&603.....................357......=..&....84.....451&.810........93...*...
+.....................*...87.........................................................447........@............*................-......*...117.
+......205.593........298..................................622....955.......34*325......*....965...................604......729....509.......
+........=..*.......+...............966.......................$.$...................206......................99....-....424..............$...
+378..........636..141.543%....34...........437-.................303.........80....*.............915...........*........*...885...........148
+............*................=......................39......-.......208.560*...906...................320.860.975....532....*.........740....
+....834...13...................426.395*962...44.......-......552...................*539.+........953.......%............647.........*.......
+..............589...............*..........................................179...71.....207........#.577......758...247..........958........
+....930.......*......&139...820.56.....%164....................536.=31......*.............................................89................
+.....#.........569............*.............882....296......&................465.......21............=.....331........998*..................
+..........847..............206....$............%.....#.464.318..=.....751*........................501..971...*..............227.......854...
+.466......*......768..../.......110......................*.......612......551......868.......727.......*......39.850..-.....*.........*.....
+.........377.561....&.718...94............246..........159...........255............%.........*...........445........472...520.......304.472
+...............*..........................@.....292.............................476......&.........135......*...223.........................
+..............288.....................944........*......775*934................+......152.............*55..806............239#..............
+...181*552...............................*....238...............499...880.476...................................417.#66.........472.........
+.....................*........%...629.853............*......@9...@......=.....443...673........271...*..........-...........98..............
+.........567*288...10.257..837......*.........862..14................21......*...................%..714...........963.........%.450....528..
+....................................862..................889.....604...*424..350...812..34........................*...646...................
+...............*....*933.....355@..................*528....=......................+.....*...&996..493.....992..840.......*..271.............
+..............984...............................678.................+571.....648......262............*......*..............*......293..197..
+..........386..............463..984......129................-588..............*..361...............408..269.652...........406........*..+...
+.........*............494....*.......847...*..#..942..825.............&......32.........................*........427...........-....339.....
+.......415.=434..439.....*..23........$..34..410.........+.......746.976.............731..../.1*.........709.......*.........197........#...
+................*......493.......760.............................................654*....398.........158....../......396.............922....
+.....845.=....955...................*.....*574....954*........@...........$.......................-....*.....796......-...426...$732........
+.......*.88.............@.........738..924............213...40....115......315...................299...108...................*.......320....
+....567...............%..13.................*14...........$.......@.....%.........../...518.......................-.810...514..952..........
+........892...275....485.......820.......367.......@.....544...........428.........54..-.............*....108..964...*..........*...........
+.......@...................861......#.........893.116.........836.982......961.............../...615.188.....*.....914.........761....765...
+...........169.962.........*......81...769...#...............................-..714.424..759.390...@.........855...........562.........*....
+....../906......../......81..968.......-..............................185..............*.%............=......................*.205.....72...
+................*............*....110........./.907....241.......708...*...383.701..869...............346..............649.771.#............
+.............531.976...670...288...........382....*.......*116.........820.-...*............764..556...........@..984....$.......638...165..
+........................-..........479.............55..........................515.....345...#................533...*.......................
+.....$....569.936.301.........914..*.......-.....=................453..............209..*..........................551.-.......479..........
+..233.....*......*.....506.........931..246....221.639....#..................@......*....784........591.................818...%.......523...
+.......233....%.........................................416...714&........448.......698......387........432............................*....
+...........718.......@......465....................430........................603.......491..*..........*...22.164...+...............179....
+...............450.462.549.=....283.185...880......................652.......................142..430.372.....*.......633...................
+.370....143....*.........*.........*......*.....962...........*.....*....................537........%...........&136........................
+.....39.......956......213...........476.143.......*.......321.60....708..........966.......#...921......+146............+...665............
+...........................941...321*.............880.499...........................*.............*.383........668.....212...*....827*428...
+..236..527...................=..........................*........../..854......................489...-........................234...........
+.....*.................................622..........289..599....207...*............&953.599=...............885............682.....914.....12
+793...754.............$497..............*............*................163....................+.........15.....*..*....338*..................
+...*.......864................348..........758.......439...................1........83....748...............325.535...................=.....
+...368....*........382*250....*.....317.......*................473*....877.+.........*..................@................%....331..513......
+.........55..239...........2...377...=......1.792...294*607........831..........*.950...&....-....#....864.....139.......512................
+....427.......*...*...#....%...........765...............................418.995.........6..492.465...........@....................*218.....
+......*......939.410.371......#...-......#.................&............*.............................496..........503..........629.........
+.....410....................215.193.................678.786.....370...71..598......329..........262.................*.......473.............
+.........979..855..432=................212.....346..*....................*........*............#...................249...=.....+...525.217..
+.........*....*...............387.....*...........*.228......./....%....245..&.....690.....429............................685.........*.....
+129....907.423........949.772......666....173..679........992.313...398......427..............*...................244.......................
+.........................*.....*............................*........................../....998..........510..825*..........................
+.....132....867........%.....42.780..............776*744...898.......664....196....360..823......899.211..=.................+...............
+.....*.......%......703................916...%...............................*....*................*....&.....689..$......591.......64*238..
+...684..160.......................445.....*...164.......602....249...........431.12.............=..758............138..........983..........
+..........@..766.92..............*......334........671..*...............&646..................797.............950.........935.........469...
+................*..................510..............*..285..........................................650.......*....807...*............#.....
+..205.669...768...88........................+59..148............682........#.................3..............376...*.......191...............
+.........&....*..%........................................183......*......61................/........657..........163............255........
+..674.......46..............392..251....507.................*.248...74..........+...775.97.....222.........%..........135...950.....*.......
+....*...........*967..533...*............*......949..245.276...#.........*.....728...................677..137.....*.....*..&........522.....
+...367.......196.......$..239..........510.748..*............/...481..722....................800........*........735..268...................
+.........442...................111...............752........567...*.........*..........696..%............790................................
+.............182....677..613............................/..........814...372.86........*......915*..............799.................*....253
+....&....739*........../....*206.........284...........584...427................727..485..........781.992.........-...............619.......
+.....603.........................907........*....607......../.............................617............*...=.......986-.............918...
+.........819..667..341...........*....#..397................................#........$..................822...20...........359.....43.......
+.......&....*...#....*.......*....88.691.........................700.675....910...227........350...&591............301.......$.......*......
+..404$..259.......557....876.218...........216.=211.7...=569........*........................=.................320..*....462...480....747...
+..........................+.............#..*........*.................=518........................274............@...276........../.........
+.......#...904......622..............923....543....969../..................571..778......#888.253..................+.....163.........-679...
+295.207.........708......................................942...496....#...*.......*.373@.......*..645...............580.......177...........
+................................982*317............304........*.......28.823....994.......370..96........@.=...*675............*.....604....
+644.138.522......%......901.................374*..*.........458.....................#135....=..........730.355........955...=.47.277*.......
+...*....*.........288..*.............+.433.........240.................317/........................609............#90.*...623...............
+.....%..675....87.......110...126.408..*......355.............................@131......110....778...*..519.834.......537.....19........502.
+..831............*460........*..........824.....*..751...467.829..........850..........*............365....*................................
+..........398..............473....-..........751.....*......*................@.446*....780...........................732....................
+.......................#.........333..................170........596...............943......66..186......@...........@.....=................
+......124....780.....58.....933........926.../.............693...*..........*828...............*........666.............=..337..............
+.......*.........197..........$.......*......67............-...335...........................122...582................197..........328......
+.....151...............................763......180....@............890../....835...578..871..........*295..#....%...........-541.$.........
+........................153.........-........@.....*...661..*833......*.775.....-...........................40....665...............701.....
+..890....252......................544........809..425..............925......................................................................