Skip to content

Instantly share code, notes, and snippets.

@schmalz
Last active September 19, 2023 15:13
Show Gist options
  • Save schmalz/848f48ce4fdf35b34c388781ae739f62 to your computer and use it in GitHub Desktop.
Save schmalz/848f48ce4fdf35b34c388781ae739f62 to your computer and use it in GitHub Desktop.
Advent of Code 2015; Day 1.
(defparameter *instruction->move*
'((#\( . 1+)
(#\) . 1-))
"Map instructions to floor movements.")
(defun all-floors (instructions)
"Return a sequence of all floors passed through when starting at floor 0 and
following the list of INSTRUCTIONS."
(reverse (reduce #'(lambda (acc i)
(cons (funcall (cdr (assoc i *instruction->move*))
(car acc))
acc))
instructions
:initial-value '(0))))
(defun where-is-santa (file)
"If Santa starts at floor 0 and follows the instructions in FILE, at which
floor does he finish?"
(car (last (all-floors (uiop:read-file-line file)))))
(defun first-basement-instruction (file)
"If Santa starts at floor 0 and follows the instructions in FILE, what is
the ordinal number of the instruction that he has just followed when he
first enters a basement floor (floor < 0)?"
(position -1 (all-floors (uiop:read-file-line file))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment