Created
September 14, 2023 16:54
-
-
Save schmalz/0a0edfc65d25dccd6fb470a15018634f to your computer and use it in GitHub Desktop.
Advent of Code 2015; Day 3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun next-house (house i) | |
(destructuring-bind (x y) house | |
(case i | |
(#\> (list (+ x 1) y)) | |
(#\< (list (- x 1) y)) | |
(#\^ (list x (+ y 1))) | |
(t (list x (- y 1)))))) | |
(defun count-unique-deliveries-by-santa (file) | |
(with-open-file (stream file) | |
(let ((instructions (read-line stream))) | |
(loop for i across instructions | |
for houses = (list (list 0 0)) then (cons (next-house (car houses) | |
i) | |
houses) | |
finally (return (length (delete-duplicates houses :test #'equal))))))) | |
(defun count-unique-deliveries-by-santa-and-robot (file) | |
(with-open-file (stream file) | |
(let ((instructions (read-line stream)) | |
(santa-houses (list (list 0 0))) | |
(robot-houses (list (list 0 0)))) | |
(loop for i across instructions | |
for idx from 0 | |
when (evenp idx) do (setf santa-houses | |
(cons (next-house (car santa-houses) | |
i) | |
santa-houses)) | |
when (oddp idx) do (setf robot-houses | |
(cons (next-house (car robot-houses) | |
i) | |
robot-houses))) | |
(length (union santa-houses robot-houses :test #'equal))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment