-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.lisp
50 lines (43 loc) · 1.83 KB
/
day9.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
;;;; day9.lisp
(in-package :advent-of-code-2018)
(defun day9-parse-input ()
(let ((input (split-seq (first (read-puzzlefile "input9.txt")) #\Space)))
(list (parse-integer (first input))
(parse-integer (nth 6 input)))))
(defun day9-make-board ()
(let ((zero (list 0)))
(setf (cdr zero) zero)))
(defun day9-insert-after (board marble)
(let ((new (list marble)))
(setf (cdr new) (cdr board)
(cdr board) new)))
(defun day9-remove-next (board)
(let ((removed (cdr board)))
(setf (cdr board) (cddr board))
removed))
(defun day9-play (players last-marble)
(declare (optimize (debug 3) (safety 3)))
(loop
:with scores := (make-hash-table)
:with board := (day9-make-board)
:with trailing := board
:with length := 1
:for marble :from 1 :upto last-marble
:when (= marble 8) :do (setf trailing board)
:unless (> 5 (mod marble 23)) :do (setf trailing (cddr trailing))
:unless (= 0 (mod marble 23))
:do (setf board (day9-insert-after (cdr board) marble))
:and :do (incf length)
:else :do (let ((removed (day9-remove-next trailing)))
(setf board (cdr removed) trailing (cdr removed))
(incf (gethash (mod marble players) scores 0) (+ marble (car removed)))
(decf length)
;(format t "~a points for player ~a.~%" (+ marble (car removed)) (1+ (mod marble players)))
)
:finally (return (multiple-value-list (max-key scores)))))
(defun day9 ()
(destructuring-bind (players marbles) (day9-parse-input)
(destructuring-bind (player points) (day9-play players marbles)
(format t "The first game wins elf #~a with ~a points.~%" player points))
(destructuring-bind (player points) (day9-play players (* 100 marbles))
(format t "The second game wins elf #~a with ~a points.~%" player points))))