Skip to content

Instantly share code, notes, and snippets.

@pavloo
Created December 9, 2017 15:24
Show Gist options
  • Save pavloo/9973ce2b8b7bf58dbde21409463b9b07 to your computer and use it in GitHub Desktop.
Save pavloo/9973ce2b8b7bf58dbde21409463b9b07 to your computer and use it in GitHub Desktop.
(defun size-of-square (N)
(let ((d 1))
(loop
(when (>= (* d d) N) (return d))
(setq d (+ d 2))
)
)
)
(defun generate-empty-square (size)
(let ((list2d '()) row (n size))
(dotimes (i n)
(setq row ())
(dotimes (j n)
(push 0 row)
)
(push row list2d)
)
list2d
)
)
(defun set-ij (list2d i j val)
(setcar (nthcdr j (nth i list2d)) val)
list2d
)
(defun get-ij (list2d i j)
(nth j (nth i list2d))
)
(defun is-not-visited (list2d i j)
(= (get-ij list2d i j) 0)
)
(defun find-manhattan-path (N)
"Solution for Part 1 http://adventofcode.com/2017/day/3"
(let ((list2d (generate-empty-square (size-of-square N))) i j center-ij direction)
(setq center-ij (/ (length list2d) 2))
(setq i center-ij)
(setq j center-ij)
(set-ij list2d i j 1)
(setq direction 'RIGHT)
(loop
for val from 2 to N
do (progn
(cond
((eq direction 'RIGHT)
(progn
(setq j (1+ j))
(when (is-not-visited list2d (1- i) j) (setq direction 'UP))
)
)
((eq direction 'UP)
(progn
(setq i (1- i))
(when (is-not-visited list2d i (1- j)) (setq direction 'LEFT))
)
)
((eq direction 'LEFT)
(progn
(setq j (1- j))
(when (is-not-visited list2d (1+ i) j) (setq direction 'DOWN))
)
)
((eq direction 'DOWN)
(setq i (1+ i))
(when (is-not-visited list2d i (1+ j)) (setq direction 'RIGHT))
)
)
(set-ij list2d i j val)
)
)
(+ (abs (- center-ij i)) (abs (- center-ij j)))
)
)
(defun sum-all-adjacent (list2d i j)
(+
(get-ij list2d (1- i) (1+ j))
(get-ij list2d i (1+ j))
(get-ij list2d (1+ i) (1+ j))
(get-ij list2d (1+ i) j)
(get-ij list2d (1+ i) (1- j))
(get-ij list2d i (1- j))
(get-ij list2d (1- i) (1- j))
(get-ij list2d (1- i) j)
)
)
;; size is the size of array to preallocate so that algo converges
;; it's taken approx to make sure spiral pattern fits it
(defun find-manhattan-path1 (N size)
"Solution for Part 2 http://adventofcode.com/2017/day/3"
(let ((list2d (generate-empty-square size)) i j center-ij direction val)
(setq center-ij (/ (length list2d) 2))
(setq i center-ij)
(setq j center-ij)
(set-ij list2d i j 1)
(setq direction 'RIGHT)
(dotimes (ii (* size 2))
(progn
(cond
((eq direction 'RIGHT)
(progn
(setq j (1+ j))
(when (is-not-visited list2d (1- i) j) (setq direction 'UP))
)
)
((eq direction 'UP)
(progn
(setq i (1- i))
(when (is-not-visited list2d i (1- j)) (setq direction 'LEFT))
)
)
((eq direction 'LEFT)
(progn
(setq j (1- j))
(when (is-not-visited list2d (1+ i) j) (setq direction 'DOWN))
)
)
((eq direction 'DOWN)
(setq i (1+ i))
(when (is-not-visited list2d i (1+ j)) (setq direction 'RIGHT))
)
)
(setq val (sum-all-adjacent list2d i j))
(set-ij list2d i j val)
(when (>= val N) (return val))
)
)
)
)
(find-manhattan-path 265149)
(find-manhattan-path1 265149 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment