aboutsummaryrefslogtreecommitdiff
path: root/2024/11/puzzle-1.lisp
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-12-11 11:07:10 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-12-11 11:07:10 +0100
commit983c4a10344dc0416d9e87d955eaab56c5db9292 (patch)
treec27467dbc9cbfe7fb8483565c44fa5f413749c7b /2024/11/puzzle-1.lisp
parenta7ad1d1097f08554d09da1885dff40760f44e34d (diff)
Add 2024 day 11 part 1 solution
Diffstat (limited to '2024/11/puzzle-1.lisp')
-rwxr-xr-x2024/11/puzzle-1.lisp51
1 files changed, 51 insertions, 0 deletions
diff --git a/2024/11/puzzle-1.lisp b/2024/11/puzzle-1.lisp
new file mode 100755
index 0000000..3fc7df7
--- /dev/null
+++ b/2024/11/puzzle-1.lisp
@@ -0,0 +1,51 @@
+#!/usr/bin/sbcl --script
+
+(defparameter *numbers*
+ '())
+
+(defun main (filename)
+ (setq *numbers* (read-stones filename))
+ (loop repeat 25
+ do (blink)
+ finally (return (length *numbers*))))
+
+(defun read-stones (filename)
+ (with-open-file (stream filename)
+ (let ((contents (make-string (file-length stream))))
+ (read-sequence contents stream)
+ (read-from-string (concatenate 'string "(" contents ")")))))
+
+(defun blink ()
+ (setq *numbers*
+ (loop for number in *numbers*
+ for after-blink = (stone-change-or-split number)
+ if (listp after-blink)
+ append after-blink
+ else
+ collect after-blink
+ finally (setq *numbers* after-blink))))
+
+(defun stone-change-or-split (number)
+ (cond ((= number 0)
+ 1)
+ ((evenp (digit-count number))
+ (number-split number))
+ (:else
+ (* 2024 number))))
+
+(defun digit-count (number)
+ (loop with x = 0
+ while (/= number 0)
+ do (progn
+ (incf x)
+ (setq number (floor number 10)))
+ finally (return x)))
+
+(defun number-split (number)
+ (let* ((length (digit-count number))
+ (string (write-to-string number))
+ (half (/ length 2)))
+ (list (parse-integer (subseq string 0 half))
+ (parse-integer (subseq string half (length string))))))
+
+(format t "~d~%" (main "input")) \ No newline at end of file