From 135ce20d06bdee95299fa4ad1c100da4ddd2a4e4 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Fri, 16 Dec 2022 17:02:52 +0100 Subject: solve ex can't use named-let run out of stack use while --- AoC2022/14/solver.el | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) (limited to 'AoC2022/14') 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) ) -- cgit v1.2.3