Skip to content

Latest commit

 

History

History
122 lines (120 loc) · 3.46 KB

2024.03.org

File metadata and controls

122 lines (120 loc) · 3.46 KB

Day 03

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-2024-03)
  4. Typing C-c C-c in the block answers

Initial stuffs

Packages to load

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

Create package for this day

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

Input

(defun process-stream (in)
  (loop for line = (read-line in nil)
        while line
        collect  (cl-ppcre:all-matches-as-strings "mul\\(\\d+,\\d+\\)|do\\(\\)|don't\\(\\)" line)))
(defun read-input (file)
  (with-open-file (in file)
    (apply #'append (process-stream in))))
(defparameter *input*
  (read-input "input/03.txt"))

Part 1

(defun multiply (operation)
  (apply #'* (mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" operation))))
(defun part-1 (operations)
  (loop for operation in operations
        for multiply = (string= "mul" (subseq operation 0 3))
        when multiply
          sum (multiply operation)))
(defun problem-a () (format t "Problem 03 A: ~a~%" (part-1 *input*)))

Part 2

(defun compute (operations)
  (loop for operation in operations
        with enabled = t
        for multiply = (string= "mul" (subseq operation 0 3))
        when (and multiply enabled)
          sum (multiply operation)
        when (string= "do()" operation)
          do (setf enabled t)
        when (string= "don't()" operation)
          do (setf enabled nil)))
(defun problem-b () (format t "Problem 03 B: ~a~%" (compute *input*)))

Putting it all together

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

Answer

Problem 03 A: 170778545
Problem 03 B: 82868252

Test Cases

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

(run! 'aoc.2024.03)

Test Results

Thoughts