Skip to content

Latest commit

 

History

History
164 lines (158 loc) · 4.18 KB

2022.02.org

File metadata and controls

164 lines (158 loc) · 4.18 KB

Day 02

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-02)
  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-02
  (:use :common-lisp
        :parseq
        :fiveam)
  (:export :problem-a
           :problem-b))
(in-package :aoc-2022-02)

Input

(defun process-input-stream (in)
  (loop for line = (read-line in nil)
        while line
        collect (list (elt line 0) (elt line 2))))

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

Part 1

(defvar *play-map*
  '((#\A . :rock)
    (#\B . :paper)
    (#\C . :scissors)
    (#\X . :rock)
    (#\Y . :paper)
    (#\Z . :scissors)))

(defvar *choice-score*
  '((:rock . 1)
    (:paper . 2)
    (:scissors . 3)))

(defvar *a-beats-b*
  '((:rock . :scissors)
    (:paper . :rock)
    (:scissors . :paper)))
(defun play-map (c)
  (cdr (assoc c *play-map*)))
(defun choice-score (choice)
  (cdr (assoc choice *choice-score*)))
(defun score-round (a b)
  (+ (choice-score b)
     (cond
       ((equalp a b) 3)
       ((equalp a (car (rassoc b *a-beats-b*))) 0)
       (t 6))))
(defun total-score (rounds)
  (loop for (them me) in rounds
        sum (score-round (play-map them) (play-map me))))
(defun problem-a () (format t "Problem 02 A: ~a~%" (total-score *input*)))

Part 2

(defun play-map-real (them choice)
  (case choice
    (#\X (cdr (assoc them *a-beats-b*)))
    (#\Y them)
    (#\Z (car (rassoc them *a-beats-b*)))))

(defun total-score-real (rounds)
  (loop for (them me) in rounds
        for opp = (play-map them)
        sum (score-round opp (play-map-real opp me))))
(defun problem-b () (format t "Problem 02 B: ~a~%" (total-score-real *input*)))

Putting it all together

<<read-input>>
<<input>>
<<initialize>>
<<structs>>
<<functions>>
<<input>>
<<play-map>>
<<problem-a>>
<<problem-b>>
(problem-a)
(problem-b)

Answer

Problem 02 A: 11666
Problem 02 B: 12767

Test Cases

(def-suite aoc.2022.02)
(in-suite aoc.2022.02)
(defvar *sample-input*
  "A Y
B X
C Z")

(test sample-test
  (with-input-from-string (in *sample-input*)
    (let ((rounds (process-input-stream in)))
      (is (= 15 (total-score rounds)))
      (is (= 12 (total-score-real rounds))))))
(run! 'aoc.2022.02)

Test Results

Running test suite AOC.2022.02
 Running test SAMPLE-TEST ..
 Did 2 checks.
    Pass: 2 (100%)
    Skip: 0 ( 0%)
    Fail: 0 ( 0%)

Thoughts