From e8ca549ac5e445f193e18ddb1767bc030cb62c58 Mon Sep 17 00:00:00 2001 From: Samuel Ortion Date: Wed, 27 Nov 2024 17:44:27 +0100 Subject: [PATCH] day3: lisp, part2 --- 2015/days/03/walk2.lisp | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 2015/days/03/walk2.lisp diff --git a/2015/days/03/walk2.lisp b/2015/days/03/walk2.lisp new file mode 100644 index 0000000..bbca8cf --- /dev/null +++ b/2015/days/03/walk2.lisp @@ -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)