-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday21.lisp
More file actions
66 lines (55 loc) · 1.92 KB
/
day21.lisp
File metadata and controls
66 lines (55 loc) · 1.92 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
(defpackage :aoc/2019/21 #.cl-user::*aoc-use*)
(in-package :aoc/2019/21)
(defstruct (droid (:constructor make-droid%))
program
in
out)
(defun make-droid (program)
(let* ((program (intcode:make-program (copy-hash-table (intcode:program-memory program)))))
(make-droid% :program program
:in (intcode:program-in program)
:out (intcode:program-out program))))
(defun droid-configure (d code)
(loop
:for instruction :in code
:do (loop
:for c :across instruction
:do (enqueue (char-code c) (droid-in d))
:finally (enqueue (char-code #\Newline) (droid-in d)))))
(defun droid-run (d code)
(droid-configure d code)
(intcode:program-run (droid-program d))
(loop
:until (queue-empty-p (droid-out d))
:for v = (dequeue (droid-out d))
:when (>= v 255) :return v
:do (format T "~a" (code-char v))))
(define-solution (2019 21) (program intcode:read-program)
(values
;; jump if:
;; - the next tile is empty
;; - the third tile is empty, but the fourth is not
(let ((code '("NOT A T"
"NOT C J"
"AND D J"
"OR T J"
"WALK")))
(droid-run (make-droid program) code))
;; jump if:
;; - the next tile is empty
;; - the third tile is empty, the fourth is not, the eight is not
;; (there was a case where you would land on a single tile,
;; be forced to jump and land on a hole!)
;; - there is a hole 2 tiles ahead, but it's safe to land in 4, so we can
;; jump immediately
(let ((code '("NOT A T"
"NOT C J"
"AND D J"
"AND H J"
"OR J T"
"NOT B J"
"AND D J"
"OR T J"
"RUN")))
(droid-run (make-droid program) code))))
(define-test (2019 21) (19355391 1143770635))