If you have a lisp installation, emacs, org-mode, and org-babel support for lisp installed you can run this by:
- Starting slime (
M-x slime
) - Typing
C-c C-c
in the block initialize. - In the repl type
(in-package :aoc-2022-13)
- Typing
C-c C-c
in the block answers
(unless (find-package :priority-queue)
(ql:quickload "priority-queue"))
(unless (find-package :cl-ppcre)
(ql:quickload "cl-ppcre"))
(unless (find-package :parseq)
(ql:quickload "parseq"))
(unless (find-package :lparallel)
(ql:quickload "lparallel"))
(unless (find-package :fiveam)
(ql:quickload "fiveam"))
(unless (find-package :series)
(ql:quickload "series"))
(unless (find-package :cl-permutation)
(ql:quickload "cl-permutation"))
(unless (find-package :bordeaux-threads)
(ql:quickload "bordeaux-threads"))
<<packages>>
(defpackage :aoc-2022-13
(:use :common-lisp
:parseq
:fiveam)
(:export :problem-a
:problem-b))
(in-package :aoc-2022-13)
(defun parse-line (line)
(read-from-string
(cl-ppcre:regex-replace-all "\\]" (cl-ppcre:regex-replace-all "\\[" (cl-ppcre:regex-replace-all "," line " ") "(") ")")))
(defun process-stream (in)
(loop for left = (read-line in nil)
for right = (read-line in nil)
while (and left right)
collect (list (parse-line left) (parse-line right))
do (read-line in nil)))
(defun read-input (file)
(with-open-file (in file)
(process-stream in)))
(defparameter *input*
(read-input "input/13.txt"))
(defun in-order-p (left right)
(cond ((and (integerp left)
(integerp right))
(cond ((< left right) -1)
((= left right) 0)
(t 1)))
((and (listp left) (integerp right))
(in-order-p left (list right)))
((and (integerp left) (listp right))
(in-order-p (list left) right))
((and (null left) (null right)) 0)
((and (null left) (not (null right))) -1)
((and (not (null left)) (null right)) 1)
((and (listp left)
(listp right))
(loop for l in left
for r in right
for ordered? = (in-order-p l r)
while (zerop ordered?)
finally (return (case ordered?
(0 (cond ((= (length left) (length right))
0)
((< (length left) (length right))
-1)
(t 1)))
(otherwise ordered?)))))
(t 0)))
(defun tally-ordered-packets (packets)
(loop for (left right) in packets
for i from 1
if (= -1 (in-order-p left right))
sum i))
(defun problem-a () (format t "Problem 13 A: ~a~%" (tally-ordered-packets *input*)))
(defun flatten-input (input)
(loop for (a b) in input
collect a
collect b))
(defun sort-packets (packets)
(sort packets (lambda (a b) (= -1 (in-order-p a b)))))
(defun decoder-key (packets)
(let* ((d1 '((2)))
(d2 '((6)))
(sorted (sort-packets (append (flatten-input packets) (list d1 d2)))))
(* (1+ (position d1 sorted :test #'equalp))
(1+ (position d2 sorted :test #'equalp)))))
(defun problem-b () (format t "Problem 13 B: ~a~%" (decoder-key *input*)))
<<read-input>>
<<input>>
<<initialize>>
<<structs>>
<<functions>>
<<input>>
<<problem-a>>
<<problem-b>>
(problem-a)
(problem-b)
Problem 13 A: 4894 Problem 13 B: 24180
(def-suite aoc.2022.13)
(in-suite aoc.2022.13)
(defparameter *sample*
"[1,1,3,1,1]
[1,1,5,1,1]
[[1],[2,3,4]]
[[1],4]
[9]
[[8,7,6]]
[[4,4],4,4]
[[4,4],4,4,4]
[7,7,7,7]
[7,7,7]
[]
[3]
[[[]]]
[[]]
[1,[2,[3,[4,[5,6,7]]]],8,9]
[1,[2,[3,[4,[5,6,0]]]],8,9]")
(defparameter *sample-input*
(with-input-from-string (in *sample*)
(process-stream in)))
(run! 'aoc.2022.13)
Running test suite AOC.2022.13 Didn't run anything...huh?