day3: lisp, part2
This commit is contained in:
parent
1f4534f41b
commit
e8ca549ac5
|
@ -0,0 +1,62 @@
|
||||||
|
;; 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)
|
Loading…
Reference in New Issue