src/2017/days/day-03.lisp @ 9b6e236a27a0

2019/06
author Steve Losh <steve@stevelosh.com>
date Fri, 06 Dec 2019 18:58:39 -0500
parents cd781337a694
children 0a241f64eebb
(defpackage :advent/2017/03 #.cl-user::*advent-use*)
(in-package :advent/2017/03)


(defun manhattan-distance (a b)
  (let-complex ((ax ay a)
                (bx by b))
    (+ (abs (- ax bx))
       (abs (- ay by)))))

(defun neighbors (coord)
  (iterate (for (dx dy) :within-radius 1 :skip-origin t)
           (collect (+ coord (complex dx dy)))))

(define-problem (2017 3) (data read) (552 330785)
  (values
    (manhattan-distance #c(0 0) (advent/spiral:number-coordinates data))
    (iterate
      (with memory = (make-hash-table))
      (initially (setf (gethash #c(0 0) memory) 1))
      (for n :from 2)
      (for coord = (advent/spiral:number-coordinates n))
      (for value = (summation (neighbors coord) :key (rcurry #'gethash memory 0)))
      (finding value :such-that (> value data))
      (setf (gethash coord memory) value))))