advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit bccf4579cb5252249b77b8cd45030e37feff63db
parent 9cb65e134d6824f905b15b3238f66c7ff99a6ebb
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Sat, 16 Dec 2023 19:48:34 -0500

Add 2023 day 16

Diffstat:
A2023/day16/day16.scm | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2023/day16/input.txt | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 222 insertions(+), 0 deletions(-)
diff --git a/2023/day16/day16.scm b/2023/day16/day16.scm
@@ -0,0 +1,112 @@
+(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 (parse-input filename)
+  (let ((file (open-input-file filename))
+        (contraption '()))
+    (while #t
+           (let ((line (car (%read-line file))))
+             (if (eof-object? line)
+                 (break)
+                 (set! contraption (cons (string->list line) contraption)))))
+    (reverse contraption)))
+
+(define (dir-to-dxdy dir)
+  (case dir
+    ((#\L) (cons -1 0))
+    ((#\R) (cons 1 0))
+    ((#\U) (cons 0 -1))
+    ((#\D) (cons 0 1))))
+
+(define (reflect-or-split dir target)
+  (case target
+    ((#\\) (case dir
+             ((#\L) (list #\U))
+             ((#\R) (list #\D))
+             ((#\U) (list #\L))
+             ((#\D) (list #\R))))
+    ((#\/) (case dir
+             ((#\L) (list #\D))
+             ((#\R) (list #\U))
+             ((#\U) (list #\R))
+             ((#\D) (list #\L))))
+    ((#\-) (case dir
+             ((#\L) (list #\L))
+             ((#\R) (list #\R))
+             ((#\U) (list #\L #\R))
+             ((#\D) (list #\L #\R))))
+    ((#\|) (case dir
+             ((#\L) (list #\U #\D))
+             ((#\R) (list #\U #\D))
+             ((#\U) (list #\U))
+             ((#\D) (list #\D))))))
+
+(define (between x a b)
+  (and (>= x a) (<= x b)))
+
+(define (count-energized contraption init-x init-y init-dir)
+  (let* ((x-max (1- (length (car contraption))))
+         (y-max (1- (length contraption)))
+         (beams (list (list init-x init-y init-dir)))
+         (hist-beams (make-hash-table))
+         (tile-state (make-hash-table)))
+    (hash-set! hist-beams (car beams) #t)
+    (while (> (length beams) 0)
+           (let* ((curr-beam (car beams))
+                  (curr-x (list-ref curr-beam 0))
+                  (curr-y (list-ref curr-beam 1))
+                  (curr-dir (list-ref curr-beam 2))
+                  (curr-dx (car (dir-to-dxdy curr-dir)))
+                  (curr-dy (cdr (dir-to-dxdy curr-dir))))
+             (set! beams (cdr beams))
+             (while #t
+                    (let ((next-x (+ curr-x curr-dx))
+                          (next-y (+ curr-y curr-dy)))
+                      (if (not (and (between next-x 0 x-max) (between next-y 0 y-max)))
+                          (break))
+                      (hash-set! tile-state (cons next-x next-y) #t)
+                      (let ((target (list-ref (list-ref contraption next-y) next-x)))
+                        (if (equal? target #\.)
+                            (begin (set! curr-x next-x) (set! curr-y next-y))
+                            (begin
+                              (map (lambda (next-dir)
+                                     (let ((next-beam (list next-x next-y next-dir)))
+                                       (if (not (hash-ref hist-beams next-beam))
+                                           (begin
+                                             (set! beams (cons (list next-x next-y next-dir) beams))
+                                             (hash-set! hist-beams next-beam #t)))))
+                                   (reflect-or-split curr-dir target))
+                              (break))))))))
+    (hash-count (const #t) tile-state)))
+
+(let* ((contraption (parse-input "input.txt"))
+       ;; start from impossible location in case 0 0 has mirror/splitter.
+       (energeized (count-energized contraption -1 0 #\R)))
+  ;; 7111
+  (format #t "Part 1: ~d" energeized)
+  (newline))
+
+(let* ((contraption (parse-input "input.txt"))
+       (y-max (1- (length contraption)))
+       (x-max (1- (length (car contraption))))
+       (max-energized-tiles 0))
+  (do ((y 0 (1+ y))) ((> y y-max))
+    (set! max-energized-tiles
+          (max max-energized-tiles
+               (count-energized contraption -1 y #\R)))
+    (set! max-energized-tiles
+          (max max-energized-tiles
+               (count-energized contraption (1+ x-max) y #\L))))
+  (do ((x 0 (1+ x))) ((> x x-max))
+    (set! max-energized-tiles
+          (max max-energized-tiles
+               (count-energized contraption x -1 #\D)))
+    (set! max-energized-tiles
+          (max max-energized-tiles
+               (count-energized contraption x (1+ y-max) #\U))))
+  ;; 7831
+  (format #t "Part 2: ~d" max-energized-tiles)
+  (newline))
diff --git a/2023/day16/input.txt b/2023/day16/input.txt
@@ -0,0 +1,110 @@
+\................\......-.........................../..............|.\..-.................-......../..........
+..\................\...........|.|../......\......../../...........|.......\....../.......\....../............
+|......|./......../..........|\.......|/..|...............................|...............|...................
+...................\.-.............................../.......\.....\..............././.......\...........|....
+................\......................|...|\............../........./...................\......./......./....
+-......\/..\/../...............................................\....\........\...............-....\......-....
+....\......-......................-..|...........................-.|...-..........\................./.........
+.......|..........\....|/................................/........................-/.\...-/................../
+..\...-..../......................\./..........|-/......\...............|...|.............................|...
+\................................\....|...........|......................|..........|.............../........-
+|..../......\.....\................../............../..\.....|..............................................-.
+.\...........\.-....|.....-..|......|..............\.....\...-......./..................../\....|.../\........
+...........|.......-..\...............\................................../...........................\........
+.........-.................../...................../.-../.........|...\..........-..........\........|........
+../...../../.|......-.....|\...../.......\......-.........-............./....................../..............
+.-....../.............-...../...........\........-.......\...............-|......\.....|....\.................
+......................./..............................\.....\...-................................./...........
+./.....\.........\-..-...-..\...................\......./....|.|.-..........|......./.........................
+.................................-...............\...../....../..\..-......................|..................
+...\.................\...\............\-.....................\...........-..............................-.....
+.....-....-|......\..../...........-.................../............-.................../............-....|...
+.....-..\.....................-............/..../..........-.../..........................-.......\...........
+..|..........................-................|-..-..............|....................../.....|......|........
+.\...........................\......//.....-..-............/.....................................././........-
+....../...\.......................\/.\.../.........\......................................-.....\......./.....
+.....|........../..................................../....-....................|....\........-.........../../.
+.............|....\...................................|.....|./..........|...........................-.|......
+......................................./........./..................-.......|...............\..../....|.......
+.../....//.\...............\.....|..|...-......|.......-|.../.-....-..\........\...../.....|..................
+............./...................................|.............../.|...................-..........-...........
+...../....|.................................................|........\...........................-....../.|...
+...\.....\.....................|/.-....\.....|.|...--...../...\......................../.........|............
+../...................../|................/......\...|..\|............-../........-............./......./.....
+...............|...................\-.................\......-/.|......./.......-....../........../....-......
+.-/-.....|...|......../...\.|........-....................\..-.......-.............................|.-/.\.....
+................-......|.....\...--................./....-..|.../............./.....\........../.........|....
+.................\...\...........|........................\...............././........|\......./.............-
+......\....\..................................././...|..|...............-.....||..............................
+..../|.\-............./././....../-./.-..........-...............-..\...........|................../..........
+..........\.-....|....\.|...............\.......\.........\./.....|...|........|.........-.................|-.
+.|.\......................\......-....-....-...............\.....-.\............./|-...-./....../.......-.....
+.........\......../.|.-.../.-...............-..-.......|......\...\..|..................................../...
+/./.......|.\.....................................-...-.|......-/...........-..................../.........\..
+...-..\........-................\...|.|....../...............|................................................
+.....................................-......\...-.../..\....|..................\.........................-....
+-........|...........-...........|//................|.....-..........|..-....../\..|../..-....\..\....../.....
+.|............../.\...................................\..|....|..\..............|................/...-.../-...
+........./.......|.|.................................|\....\|........\........|........|.....................\
+..\....../....|......../.......\|.............\........................./............/..../...............-...
+.....-........-............/../....-...............-.../......../../..-...........\.....\.......-...-.......-.
+........|...../-....-......\.............................-..|\.....\.....\-..............\.......|............
+......................................../.......-..\..........\.............|....-.....\/........./.|.....|...
+..............-...................-.-...-............../...../.....\./........./.-....................-.......
+................|........../\..../......................-.......-............\.|..|......../.........\-.......
+........|.........\..........................................-....||..-..\.................-....../..-.......\
+.....\.............../..\./.........\|.\.....-|..-..|...../.............../.........-.........................
+...|......................//......./..........-...................\......|...../......................|..|....
+....\..................|..............\........\........../..-.......-...-|...\../...../........-........./...
+...|\.|.......\...........\.-.................|.........\/-...............//.......\................-./......|
+.........................-/...-./........./.\.|.......\...\.........\....................\.....-....-.........
+......\..\....../.\........./..........-......./.-.............................\\.............................
+....................../......\|../....................-.................../..............-..\|.../............
+...........\.......|.|.......................|.....................\../........../.-.........|................
+...................../.||..........-..../.....-.................|\............................................
+..\...\.....-./..........|......-...../.....................................................|................\
+....................-.\............|../..............|..\\...........|.....|....-../..........-.....-.........
+............................./...............-.........................\...\.|...\.............|...-...|......
+.............|.............................|...................|...................\.................../...../
+...\.....\.....................-............................/..........|....|..\...........|.|................
+........./.......-.\./........................../...........................................|../....../|..\...
+....../.........................\...../..-...\................/...-........\....../../......|.................
+......./.-.../..-..........|......................................-....\-/.-...........|....-..\...........-..
+.....-...........||\...|.......|.................\........../-.............../................-...............
+|.-.............\............\.....-|...././..................................-|........-/..........-.....|...
+\...................\........./.\...|..........\.|......-....-.-.........\...............|.../-.........\.....
+.........../...........|.........|........................|.......................|..-.......................|
+..|..................-......-..................-..............................\\............-....|............
+..........|..........\......................................\...................-..................|..........
+...../.........|.......................................\..././...|\................/...................|......
+........\.|....................\\......|..............................\....../.-/...\.......................-.
+...........\.............|....\..|......../.....././|..../........\.....|.................\.........|...-.....
+.....................\......./\......|............../...........-..........\..................\\...........\..
+................................................\.../.....................-...../\|............\-.............
+...........\...............-........\................\................./...|....\.....................\...../.
+......\........|.....-....................\../..............-...............-..............-..................
+.....-............|....................\......\..|.....././...../\.....|...\...............................-.-
+........././......-.-..../....|..|../..................../...-......................-.......................|.
+.................................//....|............-........../.../.......................\......-...........
+................../................|...................................\......./...-............./............
+..|.............../.....-................-..|-.........../.....................\/.........\.../.........|.....
+.........................../|.../....-..\-...../................./..........................|...............\.
+.....|.........../..\........../......./........-..............|............|......................-..........
+....|......................................-..|.-../........................-..............\.........-........
+.-......-....................-.................././.........................-...|-......................|.....
+...-......./........../...........................-........\.........|......-...........-.-...................
+..........-.|.|.....\..-..-...\-..../.................|..|..-.......\..................|...|.......-..........
+\...........-|....-..........................\................................|.......|...\.......\-........./
+./........./-.-............\...-\.-.......................................\-.......................\..........
+........|..........\\.....\.-.../............-................|.............-.-..-.....\.......-.....\-.......
+...................\.......|...../.-...............\.../......../................../............\...|.........
+..\..........|........-.-.......|...../..../....-...........|............|................\/../.........../...
+...............\..|./............................................................/............/...............
+..\........|/.|......|.............\.............\.....................|.....\..........|......|..............
+.............-......|......|\....................\...|..........\\.........|.|....../..../..........\.......\.
+../..|..................-................................................//........|..../-..-/..../.\.........
+..\............./..........-..|......./-............../....|...............|.....................|.....--\....
+....-|.\..|....|............./..........\-.......|........|....|..-................................-./..\\...\
+....|..\.......................\......./.-............................||....-...................\......./.....
+.|-.......................-...........|..........\.../.......\......\....................-\./.................
+...................................................|.....................|....................................