Repository navigation
Expand file tree
/
Copy pathday18.lisp
More file actions
86 lines (67 loc) · 2.81 KB
/
Copy pathday18.lisp
File metadata and controls
86 lines (67 loc) · 2.81 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(defpackage :aoc/2021/18 #.cl-user::*aoc-use*)
(in-package :aoc/2021/18)
(defun parse-snumbers (data) (mapcar #'sread data))
(defun sread (string)
(flet ((ch->number (ch) (- (char-code ch) (char-code #\0))))
(loop for ch across string
unless (eql ch #\,) collect (if (find ch "[]") ch (ch->number ch)))))
(defun s+ (number1 number2)
(sreduce (append
(list #\[)
number1
number2
(list #\]))))
(defun sreduce (number)
(loop
(uiop:if-let (exploded (sexplode number))
(setf number exploded)
(uiop:if-let (split (ssplit number))
(setf number split)
(return number)))))
(defun sexplode (number &aux (depth 0) to-the-left)
(loop for (v . to-the-right) on number do
(cond ((numberp v) (push v to-the-left))
((eql v #\]) (push v to-the-left) (decf depth))
((and (eql v #\[) (/= depth 4)) (push v to-the-left) (incf depth))
(t (let ((left (pop to-the-right))
(right (pop to-the-right)))
(assert (and (eql v #\[) (= depth 4)))
(assert (eql (pop to-the-right) #\]))
(flet ((add-first (addend list)
(loop for (v . to-the-right) on list
collect (if (numberp v) (+ v addend) v) into vv
when (numberp v) append to-the-right into vv and return vv
finally (return vv))))
(return
(append
(reverse (add-first left to-the-left))
(list 0)
(add-first right to-the-right)))))))))
(defun ssplit (number)
(loop for (v . to-the-right) on number
if (or (not (numberp v)) (< v 10)) collect v into to-the-left
else return (append
to-the-left
(list #\[)
(list (floor v 2) (ceiling v 2))
(list #\])
to-the-right)))
(defun smagnitude (number &aux stack)
(loop for (ch . remaining) on number do
(case ch
(#\[ (push ch stack))
(#\] (let ((right (pop stack))
(left (pop stack)))
(assert (eql (pop stack) #\[))
(push (+ (* left 3) (* right 2)) stack)))
(otherwise (push ch stack))))
(pop stack))
(defun part1 (numbers) (smagnitude (reduce #'s+ numbers)))
(defun part2 (numbers)
(loop for (n1 . remaining) on numbers maximize
(loop for n2 in remaining
maximize (smagnitude (s+ n1 n2))
maximize (smagnitude (s+ n2 n1)))))
(define-solution (2021 18) (numbers parse-snumbers)
(values (part1 numbers) (part2 numbers)))
(define-test (2021 18) (3987 4500))