aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-16 17:02:52 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-16 17:02:52 +0100
commit135ce20d06bdee95299fa4ad1c100da4ddd2a4e4 (patch)
tree441de40f9ab116cd10d57b536c2e00029d4abb39
parent681bb881df884c54fb2422053feacfed2ef09324 (diff)
downloadscratch-135ce20d06bdee95299fa4ad1c100da4ddd2a4e4.tar.gz
scratch-135ce20d06bdee95299fa4ad1c100da4ddd2a4e4.tar.bz2
scratch-135ce20d06bdee95299fa4ad1c100da4ddd2a4e4.zip
solve ex can't use named-let run out of stack use while
-rw-r--r--AoC2022/14/solver.el49
1 files changed, 23 insertions, 26 deletions
diff --git a/AoC2022/14/solver.el b/AoC2022/14/solver.el
index a5cc896..749b29b 100644
--- a/AoC2022/14/solver.el
+++ b/AoC2022/14/solver.el
@@ -81,12 +81,6 @@
(let ((array (copy-sequence (grid-grid grid)))
(stride (grid-stride grid))
(y-len (grid-y-len grid)))
- ;; (cl-flet ((picture-coord
- ;; (idx)
- ;; (let ((x (mod idx stride))
- ;; (y (- y-len (/ idx stride) 1)))
- ;; (+ x (* y stride)))))
- ;; (seq-do-indexed (lambda (value idx) (aset array (picture-coord idx) value)) (grid-grid grid)))
(create-image array
'xbm t
:scale 20
@@ -94,8 +88,7 @@
:width (grid-x-len grid)
:height (grid-y-len grid)
:foreground "#ffffff"
- :background "#000000"
- )))
+ :background "#000000")))
(defun solver-draw-grid (grid)
(let ((stride (grid-stride grid)))
@@ -117,29 +110,33 @@
(solver-point 496 3 (solver-grid-create '(503 494 9 4)))
+(defun solver-simulate (grid)
+ (let ((x 500) (y 0) (drops 0))
+ (while
+ (ignore-error 'wrong-type-argument ;; went out of grid
+ (cond
+ ((= 0 (aref (grid-grid grid) (solver-point x (1+ y) grid)))
+ (cl-incf y)) ;; check next
+ ((= 0 (aref (grid-grid grid) (solver-point (1- x) (1+ y) grid)))
+ (cl-incf y)
+ (cl-decf x)) ;; check left
+ ((= 0 (aref (grid-grid grid) (solver-point (1+ x) (1+ y) grid)))
+ (cl-incf y)
+ (cl-incf x)) ;; check right
+ (t (aset (grid-grid grid) (solver-point x y grid) 2)
+ (cl-incf drops)
+ (setq x 500 y 0)))))
+ drops))
+
(let* ((instr "498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9")
+ (instr (find-file-noselect "input"))
(bounds (solver-bounds instr))
(grid (solver-grid-create bounds))
- (drops 0))
+ )
(solver-walls instr grid)
- ;; (thread-first
- ;; (solver-draw-wall grid)
- ;; (insert-image))
-
- grid
;; sim
- (named-let loop ((x 500)
- (y 0))
+ (solver-simulate grid)
- (ignore-error 'wrong-type-argument ;; went out of grid
- (cond
- ((= 0 (aref (grid-grid grid) (solver-point x (1+ y) grid))) (loop x (1+ y))) ;; check next
- ((= 0 (aref (grid-grid grid) (solver-point (1- x) (1+ y) grid))) (loop (1- x) (1+ y))) ;; check left
- ((= 0 (aref (grid-grid grid) (solver-point (1+ x) (1+ y) grid))) (loop (1+ x) (1+ y))) ;; check right
- (t (aset (grid-grid grid) (solver-point x y grid) 2)
- (cl-incf drops)
- (loop 500 0)))))
- (solver-draw-grid grid)
- drops
+ ;; (solver-draw-grid grid)
)