day-22.lisp (1428B)
1 (defpackage #:aoc/day-22 2 (:use #:cl #:aoc/utils) 3 (:export #:day-22)) 4 (in-package #:aoc/day-22) 5 6 (defun calculate-secret-number (secret n sequences) 7 (loop with local-sequences = (make-hash-table :test #'equal) 8 with last-price = 0 9 with history = nil 10 with price = 0 11 repeat n 12 for i from 1 13 do (setf secret (mod (logxor secret (* secret 64)) 16777216) 14 secret (mod (logxor secret (floor secret 32)) 16777216) 15 secret (mod (logxor secret (* secret 2048)) 16777216) 16 price (mod secret 10)) 17 (when (> i 1) 18 (push (- price last-price) history)) 19 when (> i 4) 20 do (unless (gethash history local-sequences) 21 (incf (gethash history sequences 0) price) 22 (setf (gethash history local-sequences) t)) 23 (setf history (take 3 history)) 24 do (setf last-price price) 25 finally (return secret))) 26 27 (defun best-sequence (sequences) 28 (loop for bananas being the hash-values of sequences 29 maximize bananas)) 30 31 (defun day-22 (input) 32 (loop with sequences = (make-hash-table :test #'equal) 33 for line = (read-line input nil) 34 until (null line) 35 for initial-secret = (parse-integer line) 36 sum (calculate-secret-number initial-secret 2000 sequences) into task-1 37 finally (return (values task-1 (best-sequence sequences)))))