aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-12-16 02:06:11 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-12-16 02:06:11 +0100
commit017671243a2ef10cf3012181715f7295a5800a75 (patch)
tree2babcec8d1370b674a2fda4c604b3171542d7f9a
parent0f6a20f4fdd2ae0e51ab80cad72b0cd8e3dfff26 (diff)
Support simulation
-rwxr-xr-x2024/15/puzzle-2.lisp42
1 files changed, 41 insertions, 1 deletions
diff --git a/2024/15/puzzle-2.lisp b/2024/15/puzzle-2.lisp
index bd3f8e5..3a88b03 100755
--- a/2024/15/puzzle-2.lisp
+++ b/2024/15/puzzle-2.lisp
@@ -1,18 +1,58 @@
#!/usr/bin/sbcl --script
+(defun usage (&optional option)
+ (when option
+ (format *error-output* "puzzle-2.lisp: invalid option -- `~a'~%" option))
+ (format *error-output* "Usage: puzzle-2.lisp [-s]~%")
+ (quit :unix-status 1))
+
(defparameter *map* nil)
(defparameter *moves* nil)
(defparameter *robot-pos* nil)
+(defparameter *sflag*
+ (case (length *posix-argv*)
+ (1
+ nil)
+ (2
+ (let ((arg (nth 1 *posix-argv*)))
+ (unless (or (string= arg "-s")
+ (string= arg "--simulate"))
+ (usage arg)))
+ t)
+ (otherwise
+ (usage (nth 2 *posix-argv*)))))
+
(defun main (filename)
(parse-input filename)
- (mapc #'handle-move *moves*)
+ (if *sflag*
+ (loop for move in *moves*
+ do (print-map)
+ (sleep 0.0001)
+ (handle-move move)
+ finally (print-map))
+ (mapc #'handle-move *moves*))
(loop with (my mx) = (array-dimensions *map*)
for y from 0 below my
sum (loop for x from 0 below mx
if (box-at-pos-p (cons x y))
sum (+ x (* 100 y)))))
+(defun print-map ()
+ (format t "~C[2J" #\Esc)
+ (loop with (my mx) = (array-dimensions *map*)
+ for y from 0 below my
+ append (loop for x from 0 below mx
+ append (ecase (aref *map* y x)
+ (#\@ '(#\Esc #\[ #\9 #\2 #\m #\@ #\Esc #\[ #\m))
+ (#\# '(#\Esc #\[ #\9 #\1 #\m #\# #\Esc #\[ #\m))
+ (#\. '(#\.))
+ (#\[ '(#\Esc #\[ #\3 #\4 #\m #\[ #\Esc #\[ #\m))
+ (#\] '(#\Esc #\[ #\3 #\4 #\m #\] #\Esc #\[ #\m))))
+ into string
+ collect #\Newline into string
+ finally (format t "~a" (coerce string 'string))))
+
(defun handle-move (v⃗)
(when (can-move-p v⃗)
(map-setf *robot-pos* #\.)