Skip to content

Latest commit

 

History

History
150 lines (141 loc) · 3.6 KB

2022.01.org

File metadata and controls

150 lines (141 loc) · 3.6 KB

Day 01

Executing this code

If you have a lisp installation, emacs, org-mode, and org-babel support for lisp installed you can run this by:

  1. Starting slime (M-x slime)
  2. Typing C-c C-c in the block initialize.
  3. In the repl type (in-package :aoc-2022-01)
  4. Typing C-c C-c in the block answers

Initial stuffs

Packages to load

(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"))

Create package for this day

<<packages>>
(defpackage :aoc-2022-01
  (:use :common-lisp
        :parseq
        :fiveam)
  (:export :problem-a
           :problem-b))
(in-package :aoc-2022-01)

Input

(defun parse-stream (in)
  (loop
    while (peek-char t in nil nil)
    collect
        (loop
          for line = (read-line in nil)
          while (not (string= "" line))
          while line
          collect (parse-integer line))))

(defun read-input (file)
  (with-open-file (in file)
    (parse-stream in)))
(defparameter *input*
  (read-input "input/01.txt"))

Part 1

(defun most-calories (elves)
  (reduce #'max (mapcar (lambda (elf) (reduce #'+ elf)) elves)))
(defun problem-a () (format t "Problem 01 A: ~a~%" (most-calories *input*)))

Part 2

(defun top-three-calories (elves)
  (reduce #'+ (sort (mapcar (lambda (elf) (reduce #'+ elf)) elves) #'>) :end 3))
(defun problem-b () (format t "Problem 01 B: ~a~%" (top-three-calories *input*)))

Putting it all together

<<read-input>>
<<input>>
<<initialize>>
<<structs>>
<<functions>>
<<input>>
<<most-calories>>
<<problem-a>>
<<top-three-calories>>
<<problem-b>>
(problem-a)
(problem-b)

Answer

Problem 01 A: 74711
Problem 01 B: 209481

Test Cases

(def-suite aoc.2022.01)
(in-suite aoc.2022.01)

(defvar *test-input*
  "1000
2000
3000

4000

5000
6000

7000
8000
9000

10000")

(test calories
  (with-input-from-string (in *test-input*)
    (let ((input (parse-stream in)))
      (is (= 24000 (most-calories input)))
      (is (= 45000 (top-three-calories input))))))

(run! 'aoc.2022.01)

Test Results

Running test suite AOC.2022.01
 Running test CALORIES ..
 Did 2 checks.
    Pass: 2 (100%)
    Skip: 0 ( 0%)
    Fail: 0 ( 0%)

Thoughts