Skip to content

Instantly share code, notes, and snippets.

@Arnot
Created December 4, 2017 17:04
Show Gist options
  • Save Arnot/822ac1ddecf2ddf40844f081c0ebff66 to your computer and use it in GitHub Desktop.
Save Arnot/822ac1ddecf2ddf40844f081c0ebff66 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))))
(assert (> day3-input (sum-neighbors)))
(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))
(dotimes (i (1- (* aoc-size aoc-size)))
(pp aoc-grid)
(sleep-for 0 50)
(comint-clear-buffer)
(spiral-step))
aoc-grid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment