commit 884cee487d779ed14ea96b750dbec86ba85bf9a0 (patch)
parent 486b135a0aabd6544748c50152121da0e31fb2bf
Author: Alex Karle <alex@alexkarle.com>
Date: Wed, 14 Dec 2022 23:22:50 -0500
day14: Add part 2 solution (slow, but works)
Diffstat:
1 file changed, 30 insertions(+), 2 deletions(-)
diff --git a/2022/14/sol.scm b/2022/14/sol.scm
@@ -78,6 +78,23 @@
(else
(hash-table-set! G (list x y) 'o))))
+(define (empty-incl-floor? G x y floor)
+ (not (or (hash-table-exists? G (list x y))
+ (= y floor))))
+
+(define (drop-sand-floor G x y floor)
+ (cond ((empty-incl-floor? G x (add1 y) floor)
+ (drop-sand-floor G x (add1 y) floor))
+ ; we're blocked at this point, try left/right
+ ((empty-incl-floor? G (sub1 x) (add1 y) floor)
+ (drop-sand-floor G (sub1 x) (add1 y) floor))
+ ((empty-incl-floor? G (add1 x) (add1 y) floor)
+ (drop-sand-floor G (add1 x) (add1 y) floor))
+ (else
+ (if (and (= x 500) (= y 0))
+ #f ; plugged the start -> done
+ (hash-table-set! G (list x y) 'o)))))
+
;; We map the world state as a hash table (effectively a sparse matrix)
;; so that we don't have to guess the vector dimensions first
(define (main args)
@@ -87,14 +104,25 @@
(for-each (lambda (pt)
(hash-table-set! G pt 'R)) (parse-line l)))
lines)
- (let ((cliff (apply max (map (lambda (pt) (cadr pt)) (hash-table-keys G)))))
+ (let* ((cliff (apply max (map (lambda (pt) (cadr pt)) (hash-table-keys G))))
+ (floor (+ 2 cliff)))
;; note: could also just count the number of 'o values instead of i
(let loop ((i 0))
; (print "------" i "-------")
; (print-grid G)
(if (drop-sand G 500 0 cliff)
(loop (add1 i))
- (print i))))))
+ ;; HACK: we can keep the same grid, since we stopped RIGHT at the point
+ ;; we'd hit the floor anyways--just make sure to sum your numbers together!
+ (let floop ((j 0))
+ ;; (print "------" j "-------")
+ ;; (print-grid G)
+ (if (drop-sand-floor G 500 0 floor)
+ (floop (add1 j))
+ (begin
+ (print i)
+ (print (+ i (add1 j)))))))))
+ (print-grid G)))
; For repl
(define ls '("498,4 -> 498,6 -> 496,6"