src/2021/days/day-02.lisp @ 2848a4548adf

2023/01 and 2022/01

Also start porting my test data to the new account, since Twitter imploded and
apparently it's impossible for a website to store a goddamn username and
password in The Year of Our Lord 2023 so everyone just outsources auth all
the time, ugh.
author Steve Losh <steve@stevelosh.com>
date Fri, 01 Dec 2023 11:05:43 -0500
parents bd509ececdf0
children (none)
(advent:defpackage* :advent/2021/02)
(in-package :advent/2021/02)

(defun parse (stream)
  (iterate (for line :in-stream stream :using #'read-line)
           (ppcre:register-groups-bind ((#'ensure-keyword command) (#'parse-integer n))
               ("(\\w+) (\\d+)" line)
             (collect (cons command n)))))

(defun horiz (pos) (realpart pos))
(defun depth (pos) (imagpart pos))

(defun part1 (course)
  (iterate (for (cmd . n) :in course)
           (summing (ecase cmd
                      (:forward (complex n 0))
                      (:down    (complex 0 n))
                      (:up      (complex 0 (- n)))))))

(defun part2 (course)
  (iterate (with pos = 0)
           (with aim = 0)
           (for (cmd . n) :in course)
           (ecase cmd
             (:forward (incf pos (complex n (* n aim))))
             (:down    (incf aim n))
             (:up      (decf aim n)))
           (returning pos)))

(define-problem (2021 2) (data) (1660158 1604592846)
  (let* ((course (parse data))
         (p1 (part1 course))
         (p2 (part2 course)))
    (values (* (horiz p1) (depth p1))
            (* (horiz p2) (depth p2)))))

#; Scratch --------------------------------------------------------------------