aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2023-12-12 07:07:15 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2023-12-12 07:07:15 -0500
commitb76edb1587e71376cedb4a81ad1ca4ce20ba59fb (patch)
treea945c0bbd4300b65a9bd7fcfdbdd596e8eeca6d1
parent6a634c91145fab5b8ddb3af79c393607cd6272b6 (diff)
downloadgleam_aoc-b76edb1587e71376cedb4a81ad1ca4ce20ba59fb.tar.gz
gleam_aoc-b76edb1587e71376cedb4a81ad1ca4ce20ba59fb.zip
day 12 tweaks
-rw-r--r--aoc2023-other/day-12/day-12.rkt20
-rw-r--r--aoc2023/src/utilities/array2d.gleam27
2 files changed, 38 insertions, 9 deletions
diff --git a/aoc2023-other/day-12/day-12.rkt b/aoc2023-other/day-12/day-12.rkt
index f7d303c..d07994f 100644
--- a/aoc2023-other/day-12/day-12.rkt
+++ b/aoc2023-other/day-12/day-12.rkt
@@ -16,22 +16,24 @@
(do-count template spring-group left-in-group need-gap?)
;; template: list of spring positions
;; spring-group: list of remaining contiguous groups of damaged springs
- ;; left-in-group: springs remaining in current spring group being placed
- ;; need-gap?: did we just finish placing a spring group
+ ;; left-in-group: springs remaining in current bad spring group being placed
+ ;; need-gap?: did we just finish placing a bad spring group
;; and need at least one undamaged spring before starting the next one?
(match (list template spring-group left-in-group need-gap?)
- ;; no gears left to place, this is an OK arrangement, count it
+ ;; no springs left to place and no places left to place springs
+ ;; this is an OK arrangement, count it
[(list '() '() 0 _) 1]
- ;; ambiguous wildcard, try both skipping this spot and starting a gear group here
+ ;; ambiguous wildcard, try both skipping this spot and starting a damaged spring group here
[(list (list* #\? t-rest) (list* g g-rest) 0 #f)
(+ (do-count t-rest g-rest (sub1 g) (= g 1)) (do-count t-rest spring-group 0 #f))]
- ;; definitely a place for a good spring (.); move on without consuming any spring groups
- [(or (list (list* #\? t-rest) '() 0 #f)
- (list (list* #\? t-rest) _ 0 #t)
- (list (list* #\. t-rest) _ 0 _))
+ ;; definitely a place for a good spring (.), move on without consuming any spring groups
+ [(or (list (list* #\? t-rest) '() 0 #f) ; good spring, no more damaged springs to place
+ (list (list* #\? t-rest) _ 0 #t) ; good spring right after finishing a group of bad springs
+ (list (list* #\. t-rest) _ 0 _)) ; known good spring
(do-count t-rest spring-group 0 #f)]
- ;; definitely a place for a damaged spring (#); use the next spring group and remove 1 from it
+ ;; start of bad spring (#) group, use the next spring group and remove 1 from it
[(list (list* #\# t-rest) (list* g g-rest) 0 #f) (do-count t-rest g-rest (sub1 g) (= g 1))]
+ ;; continuation of bad spring group, same
[(list (list* (or #\? #\#) t-rest) g left #f) (do-count t-rest g (sub1 left) (= left 1))]
;; if nothing above works, this arrangement's invalid
[_ 0]))
diff --git a/aoc2023/src/utilities/array2d.gleam b/aoc2023/src/utilities/array2d.gleam
new file mode 100644
index 0000000..1ad824c
--- /dev/null
+++ b/aoc2023/src/utilities/array2d.gleam
@@ -0,0 +1,27 @@
+import gleam/list
+import gleam/dict.{type Dict}
+import gleam/string
+
+pub type Posn {
+ Posn(x: Int, y: Int)
+}
+
+pub type Array2D(a) =
+ Dict(Posn, a)
+
+pub fn to_2d_array(xss: List(List(a))) -> Array2D(a) {
+ {
+ use x, row <- list.index_map(xss)
+ use y, cell <- list.index_map(row)
+ #(Posn(x, y), cell)
+ }
+ |> list.flatten
+ |> dict.from_list
+}
+
+pub fn parse_grid(str: String) -> Array2D(String) {
+ str
+ |> string.split("\n")
+ |> list.map(string.to_graphemes)
+ |> to_2d_array
+}