Skip to content

Commit

Permalink
Refactor 2023/15
Browse files Browse the repository at this point in the history
- Use DEFINE-SOLUTION / DEFINE-TEST
- Use (ENUMERATE :START), instead of bunch of 1+
  • Loading branch information
iamFIREcracker committed Jan 28, 2024
1 parent 2e76d28 commit 1000e9b
Showing 1 changed file with 15 additions and 30 deletions.
45 changes: 15 additions & 30 deletions src/2023/day15.lisp
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
(defpackage :aoc/2023/15 #.cl-user::*aoc-use*)
(in-package :aoc/2023/15)

(defun part1 (&optional (strings (uiop:read-file-lines #P"src/2023/day15.txt")))
(bnd1 (s (first strings))
(bnd1 (parts (split-sequence:split-sequence #\, s))
(looping
(dolist (each parts)
(bnd1 (value 0)
(doseq (ch each)
(setf value (rem (* (+ (char-code ch) value) 17) 256)))
(sum! value)))))))
#+#:excluded (part1)


(defun hash (s)
(bnd1 (value 0)
(doseq (ch s)
(setf value (rem (* (+ (char-code ch) value) 17) 256)))
value))
#+#:excluded (hash "rn")

(defun part1 (&optional (strings (uiop:read-file-lines #P"src/2023/day15.txt"))
&aux (s (first strings)))
(looping
(dolist (part (split-sequence:split-sequence #\, s))
(sum! (hash part)))))
#+#:excluded (part1)
; 510013
(defun part1 (&optional (s (first (aoc::read-problem-input 2023 15))))
(reduce #'+ (split-sequence:split-sequence #\, s) :key #'hash))

(defun focusing-power (boxes)
(looping
(dorange (i 0 (length boxes))
(bnd1 (box (aref boxes i))
(doseq ((j (label . num)) (enumerate (reverse box)))
(sum! (* (1+ i) (1+ j) num)))))))

(defun part2 (&optional (strings (uiop:read-file-lines #P"src/2023/day15.txt"))
&aux (s (first strings)))
(defun part2 (&optional (s (first (aoc::read-problem-input 2023 15))))
(bnd1 (boxes (make-array 256 :initial-element nil))
(dolist (part (split-sequence:split-sequence #\, s))
(cl-ppcre:register-groups-bind (label op num)
Expand All @@ -48,5 +23,15 @@
(setf (cdr it) (parse-integer num))
(push (cons label (parse-integer num)) (aref boxes i)))))))
(focusing-power boxes)))
#+#:excluded (part2)
; 268497

(defun focusing-power (boxes)
(looping
(doseq ((i box) (enumerate boxes :start 1))
(doseq ((j (label . num)) (enumerate (reverse box) :start 1))
(sum! (* i j num))))))


(define-solution (2023 15) (s first)
(values (part1 s) (part2 s)))

(define-test (2023 15) (510013 268497))

0 comments on commit 1000e9b

Please sign in to comment.