Advertisement
clockworkfrogs

Untitled

Dec 2nd, 2021
1,429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.55 KB | None | 0 0
  1. (defpackage aoc-2021-2
  2.   (:use :cl :arrow-macros :rutils.bind :rutils.symbol :rutils.readtable :lol :1am))
  3. (in-package :aoc-2021-2)
  4.  
  5. (named-readtables:in-readtable rutils.readtable:rutils-readtable)
  6.  
  7. (defun parse-command (cmd)
  8.   (bind (((direction amount) (str:split #\  cmd))
  9.          (dir (alexandria:switch (direction :test #'string=)
  10.                 ("up" :up)
  11.                 ("forward" :forward)
  12.                 ("down" :down))))
  13.     (list dir (parse-integer amount))))
  14.  
  15. (defun run-program (commands)
  16.   (loop with x = 0
  17.         with depth = 0
  18.         for (command amount) in commands
  19.         do (ecase command
  20.              (:up (decf depth amount))
  21.              (:down (incf depth amount))
  22.              (:forward (incf x amount)))
  23.            (format t "~a ~a: (~a, ~a)~%" command amount x depth)
  24.         finally (return (* x depth))))
  25.  
  26. (defun get-answer-1 ()
  27.   (->> (aoc:get-input 2021 2)
  28.     (str:lines)
  29.     (mapcar #'parse-command)
  30.     (run-program)))
  31.  
  32.  
  33. (defun run-program-aim (commands)
  34.   (loop with x = 0
  35.         with depth = 0
  36.         with aim = 0
  37.         for (command amount) in commands
  38.         do (ecase command
  39.              (:up (decf aim amount))
  40.              (:down (incf aim amount))
  41.              (:forward (progn (incf x amount)
  42.                               (incf depth (* aim amount)))))
  43.            (format t "~a ~a: (~a, ~a)~%" command amount x depth)
  44.         finally (return (* x depth))))
  45.  
  46.  
  47. (defun get-answer-2 ()
  48.   (->> (aoc:get-input 2021 2)
  49.     (str:lines)
  50.     (mapcar #'parse-command)
  51.     (run-program-aim)))
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement