day3: lisp

This commit is contained in:
Samuel Ortion 2024-11-27 17:21:07 +01:00
parent c583bfb813
commit 1f4534f41b
Signed by: sortion
GPG Key ID: 9B02406F8C4FB765
1 changed files with 51 additions and 0 deletions

51
2015/days/03/walk.lisp Normal file
View File

@ -0,0 +1,51 @@
;; 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 x 0)
(defparameter y 0)
(visit (list x y))
(defvar walk)
(defun make-move (char)
(progn
(setq walk (move char))
(setf x (+ x (nth 0 walk)))
(setf y (+ y (nth 1 walk)))
(visit (list x y))))
(with-open-file (in "./data/input")
(do ((c (read-char in)
(read-char in nil 'the-end)))
((not (characterp c)))
(make-move c)))
(print visited-houses-counter)