aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/14
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/14')
-rw-r--r--AoC2022/14/solver.el46
1 files changed, 39 insertions, 7 deletions
diff --git a/AoC2022/14/solver.el b/AoC2022/14/solver.el
index bd0464f..a5cc896 100644
--- a/AoC2022/14/solver.el
+++ b/AoC2022/14/solver.el
@@ -14,6 +14,7 @@
;;; Code:
(require 'seq)
+(require 'subr-x)
(defun solver-bounds (str-or-buffer)
(with-temp-buffer
@@ -36,7 +37,7 @@
(x-len (1+ (- (car bounds) (cadr bounds))))
(y-len (1+ (elt bounds 2)))
(stride (* 8 (1+ (/ x-len 8))))
- (grid (make-bool-vector (* stride y-len) nil))))
+ (grid (make-vector (* stride y-len) 0))))
(:copier nil))
"Represents a snapshot of game of life."
bounds
@@ -59,9 +60,9 @@
(unless (null startx)
(if (= startx fx)
(cl-loop for y from (min starty fy) to (max starty fy)
- do (aset (grid-grid grid) (solver-point startx y grid) t))
+ do (aset (grid-grid grid) (solver-point startx y grid) 1))
(cl-loop for x from (min startx fx) to (max startx fx)
- do (aset (grid-grid grid) (solver-point x starty grid) t))))
+ do (aset (grid-grid grid) (solver-point x starty grid) 1))))
(setq startx fx
starty fy)))))
@@ -96,6 +97,19 @@
:background "#000000"
)))
+(defun solver-draw-grid (grid)
+ (let ((stride (grid-stride grid)))
+ (seq-do-indexed
+ (lambda (elt idx)
+ (let ((slot (mod idx stride)))
+ (when (= 0 slot)
+ (insert "\n"))
+ (insert (cl-case elt
+ (0 ".")
+ (1 "#")
+ (2 "o")))))
+ (grid-grid grid))))
+
(solver-walls "498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9"
@@ -106,8 +120,26 @@
(let* ((instr "498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9")
(bounds (solver-bounds instr))
- (grid (solver-grid-create bounds)))
+ (grid (solver-grid-create bounds))
+ (drops 0))
(solver-walls instr grid)
- (thread-first
- (solver-draw-wall grid)
- (insert-image)))
+ ;; (thread-first
+ ;; (solver-draw-wall grid)
+ ;; (insert-image))
+
+ grid
+ ;; sim
+ (named-let loop ((x 500)
+ (y 0))
+
+ (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
+ )