(ql:quickload "split-sequence")
(defun read-data (path)
(with-open-file (stream path)
(loop for line = (read-line stream nil)
while line collect line)))
(defparameter *input-source*
(cadr *posix-argv*))
;; Useful in testing
;; (defparameter *input-source* "example_input")
;; (defparameter *input-source* "input")
(defun parse-input (lines)
(map 'list #'parse-integer
(split-sequence:split-sequence #\, (car lines))))
(defun preprocess-input (numbers)
(let ((arr (make-array 9)))
(dolist (n numbers)
(setf (aref arr n) (1+ (aref arr n))))
arr))
(defparameter *data*
(parse-input (read-data *input-source*)))
(defun solution (data days)
(let ((working (preprocess-input data)))
(dotimes (n days)
(let ((zeroes (aref working 0)))
(loop for x from 1 to 8
do (setf (aref working (1- x)) (aref working x)))
(setf (aref working 8) zeroes)
(setf (aref working 6) (+ zeroes (aref working 6)))))
(loop for x from 0 to 8 sum (aref working x))))
(defun part1 (data)
(solution data 80))
(defun part2 (data)
(solution data 256))
(format t "===== Part 1 =====")
(format t "Result: ~A~%~%" (time (part1 *data*)))
(format t "===== Part 2 =====")
(format t "Result: ~A~%" (time (part2 *data*)))