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-2024-01)
- 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-2024-01
(:use :common-lisp
:parseq
:fiveam)
(:export :problem-a
:problem-b))
(in-package :aoc-2024-01)
(defun process-stream (in)
(loop for line = (read-line in nil)
with l = (list)
with r = (list)
while line
do (with-input-from-string (s line)
(let ((left (read s)) (right (read s)))
(push left l)
(push right r)))
finally
(return (list l r))))
(defun read-input (file)
(with-open-file (in file)
(process-stream in)))
(defparameter *input*
(read-input "input/01.txt"))
(defun distance (a b) (abs (- a b)))
(defun part-1 (l r)
(loop for a in l
for b in r
sum (distance a b)))
(defun problem-a ()
(let ((left (copy-seq (first *input*)))
(right (copy-seq (second *input*))))
(format t "Problem 01 A: ~a~%" (part-1 (sort left #'<=) (sort right #'<=)))))
(defun similarity (l r)
(loop for a in l
for c = (count a r)
sum (* a c)))
(defun problem-b () (format t "Problem 01 B: ~a~%" (similarity (first *input*) (second *input*))))
<<read-input>>
<<input>>
<<initialize>>
<<structs>>
<<functions>>
<<input>>
<<problem-a>>
<<problem-b>>
(problem-a)
(problem-b)
Problem 01 A: 2264607 Problem 01 B: 19457120
(def-suite aoc.2024.01)
(in-suite aoc.2024.01)
(run! 'aoc.2024.01)
Running test suite AOC.2024.01 Didn't run anything...huh?