Skip to content

Instantly share code, notes, and snippets.

@Arnot
Created December 4, 2017 17:09
Show Gist options
  • Save Arnot/1140ca604b682338775c588c7b398d59 to your computer and use it in GitHub Desktop.
Save Arnot/1140ca604b682338775c588c7b398d59 to your computer and use it in GitHub Desktop.
(defun create-grid ()
(let ((grid (make-vector aoc-size 0)))
(dotimes (i aoc-size)
(setf (aref grid i) (make-vector aoc-size 0)))
(setf (aref (aref grid center)
center) 1)
grid))
(defun get-cell (x y)
(cond
((or (< x 0)
(< y 0)) 0)
((or (> x (1- aoc-size))
(> y (1- aoc-size))) 0)
(t (aref (aref aoc-grid y) x))))
(defun sum-neighbors ()
(let ((x (car aoc-position))
(y (cdr aoc-position)))
(+ (get-cell (- x 1) (- y 1))
(get-cell (- x 1) y )
(get-cell (- x 1) (+ y 1))
(get-cell x (- y 1))
(get-cell x y )
(get-cell x (+ y 1))
(get-cell (+ x 1) (- y 1))
(get-cell (+ x 1) y )
(get-cell (+ x 1) (+ y 1)))))
(defun spiral-step ()
(setf aoc-position (cons (+ (car aoc-position) (car aoc-direction))
(+ (cdr aoc-position) (cdr aoc-direction))))
(when (and (not found-answer)
(< day3-input (sum-neighbors)))
(print (sum-neighbors))
(setf found-answer t))
(setf (aref (aref aoc-grid (cdr aoc-position)) (car aoc-position)) (sum-neighbors))
(cond
;; Hit the right limit -> move up
((> (car aoc-position) maxright)
(incf maxright)
(setf aoc-direction '(0 . -1)))
;; Hit top limit, move left
((< (cdr aoc-position) maxtop)
(decf maxtop)
(setf aoc-direction '(-1 . 0)))
;; Hit left limit, move down
((< (car aoc-position) maxleft)
(decf maxleft)
(setf aoc-direction '(0 . 1)))
;; Hit bottom limit, move right
((> (cdr aoc-position) maxbot)
(incf maxbot)
(setf aoc-direction '(1 . 0)))))
(defun aoc-day3-2 ()
(setf day3-input 312051)
(setf aoc-size 11)
(setf center (/ aoc-size 2))
(setf aoc-direction '(1 . 0))
(setf aoc-position (cons center center))
(setf maxleft center)
(setf maxright center)
(setf maxtop center)
(setf maxbot center)
(setf aoc-grid (create-grid))
(setf found-answer nil)
(dotimes (i (1- (* aoc-size aoc-size)))
(spiral-step))
aoc-grid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment