aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--aoc/2021/day04/part01.neb77
1 files changed, 55 insertions, 22 deletions
diff --git a/aoc/2021/day04/part01.neb b/aoc/2021/day04/part01.neb
index 49b4a7a..e73a9c5 100644
--- a/aoc/2021/day04/part01.neb
+++ b/aoc/2021/day04/part01.neb
@@ -44,7 +44,13 @@
(slice lst (+ 1 idx))))
(func has-won-row (row)
- (apply and row))
+ (reduce
+ ;(apply and row))
+ (lambda (acc x)
+ (and acc
+ (int? x)))
+ row
+ #true))
(func has-won (board moves)
(def checked (check-board board moves))
@@ -69,15 +75,22 @@
(redef winner #true)))
winner)
-
-(func check-board (board moves)
- (func check-row (row)
- (map
- (lambda (x)
- (in? x moves))
- row))
- (map check-row board))
+; this could (should?) be interior to check-board
+(func check-row (row moves)
+ (map
+ (lambda (x)
+ (if (in? x moves)
+ 0 ; use an integer
+ x))
+ row))
+
+(func check-board (board my-moves)
+ (map (lambda (x) (check-row x my-moves)) board))
+
+; good candidate for stdlib
+(func extend (lst1 lst2)
+ (reduce append lst2 lst1))
; how do we better iterate through this list?
@@ -86,16 +99,36 @@
(def winner #false)
(while (not winner)
(redef cur (unzip moves (+ 1 (length (first cur)))))
- (print (->string cur))
- (print (->string
- (map
- (lambda (x) (has-won x (first cur)))
- boards)))
- ;(def checked (check-board (first boards) (first cur)))
- ;(print (->string checked))
- (if (nil? (last cur))
- (redef winner #true))
- )
-
-(print (->string moves))
-;(print (->string (unzip moves 2)))
+
+ (def win-boards
+ (drop-while boards
+ (not (has-won _item_ (first cur)))))
+
+ ; if win-boards is not empty, we found a winner!
+ (if (not (nil? win-boards))
+ (block
+ (redef winner #true)
+
+ ; result: a 25 element list of ints, where called numbers
+ ; are a 0, and uncalled numbers are their integer equivalents
+ (def winning-board
+ (map
+ (lambda (x) ; convert all strings to ints
+ (if (string? x)
+ (string->int x)
+ x))
+ (reduce extend ; flatten the board from 5x5 to 1x25
+ (check-board ; check the board, called numbers become 0
+ (first win-boards)
+ (first cur))
+ (list))))
+
+ (def sum-of-uncalled
+ (apply + winning-board))
+
+ ; benefit of zipper -- easy to get recent elements!
+ (def last-called (string->int (first (first cur))))
+
+ (print (concat "sum of uncalled: " (->string sum-of-uncalled)))
+ (print (concat "last called: " (->string last-called)))
+ (print (concat "answer: " (->string (* sum-of-uncalled last-called)))))))