AoC/2015/days/03/walk2.lisp

63 lines
1.4 KiB
Common Lisp
Raw Normal View History

2024-11-27 17:44:27 +01:00
;; Day 3: Perfectly Spherical Houses in a Vacuum
;;
;; Part 1: How many houses recieve at least one present?
(defun move (char)
(cond
((equal char #\>)
(list 1 0))
((equal char #\^)
(list 0 -1))
((equal char #\<)
(list -1 0))
((equal char #\v)
(list 0 1))
(t nil)
))
(defparameter *houses* (make-hash-table :test 'equal))
(defparameter visited-houses-counter 0)
(defun visit (house)
(if (gethash house *houses*)
"key exist"
(progn
(setf visited-houses-counter (+ visited-houses-counter 1))
(setf (gethash house *houses*) 1))))
(defparameter xsanta 0)
(defparameter ysanta 0)
(defparameter xrobot 0)
(defparameter yrobot 0)
(visit (list 0 0))
(defparameter i 0)
(defvar walk)
(defun make-move (char)
(setq walk (move char))
(setq i (+ i 1))
(if (= 0 (mod i 2))
(progn
(setf xrobot (+ xrobot (nth 0 walk)))
(setf yrobot (+ yrobot (nth 1 walk)))
(visit (list xrobot yrobot)))
(progn
(setf xsanta (+ xsanta (nth 0 walk)))
(setf ysanta (+ ysanta (nth 1 walk)))
(visit (list xsanta ysanta)))))
(with-open-file (in "./data/input")
(do ((c (read-char in)
(read-char in nil 'the-end)))
((not (characterp c)))
(if (not (equal c #\Newline))
(make-move c))))
(print visited-houses-counter)