src/2018/day-11.lisp @ a2fa45383a67

Day 13
author Steve Losh <steve@stevelosh.com>
date Sun, 16 Dec 2018 21:30:28 -0500
parents a19c9e1fd077
children 5b5c61ad8d2b
(defpackage :advent/2018/11 #.cl-user::*advent-use*)
(in-package :advent/2018/11)

(defun cell (x y)
  (complex x y))

(defun x (cell)
  (realpart cell))

(defun y (cell)
  (imagpart cell))

(defun rack-id (cell)
  (+ (x cell) 10))

(defun power-level (serial-number cell)
  (-<> (rack-id cell)
    (* <> (y cell))
    (+ <> serial-number)
    (* <> (rack-id cell))
    (nth-digit 2 <>)
    (- <> 5)))

(define-problem (2018 11) (serial-number read)
  (let ((totals (make-array (list 300 300))))
    (flet ((gref (x y)
             (let ((x (1- x))
                   (y (1- y)))
               (if (array-in-bounds-p totals x y)
                 (aref totals x y)
                 0)))
           ((setf gref) (value x y)
            (setf (aref totals (1- x) (1- y)) value)))
      (iterate (for-nested ((x :from 300 :downto 1)
                            (y :from 300 :downto 1)))
               (setf (gref x y)
                     (+ (power-level serial-number (cell x y))
                        (gref (1+ x) y)
                        (gref x (1+ y))
                        (- (gref (1+ x) (1+ y))))))
      (labels ((square-power (x y n)
                 (let ((xn (+ x n))
                       (yn (+ y n)))
                   (+ (gref x y)
                      (- (gref xn y))
                      (- (gref x yn))
                      (gref xn yn))))
               (largest-square (n)
                 (iterate
                   (for-nested ((x :from 1 :to (- 301 n))
                                (y :from 1 :to (- 301 n))))
                   (for power = (square-power x y n))
                   (finding (list x y power) :maximizing power))))
        (values (str:join "," (subseq (largest-square 3) 0 2))
                (iterate (for n :from 1 :to 300)
                         (for (x y power) = (largest-square n))
                         (finding (format nil "~D,~D,~D" x y n)
                                  :maximizing power)))))))


(1am:test test-2018/11
  (multiple-value-bind (part1 part2) (run)
    (1am:is (string= "245,14" part1))
    (1am:is (string= "235,206,13" part2))))