src/2018/day-10.lisp @ a18b7db936b8

2019/01
author Steve Losh <steve@stevelosh.com>
date Sun, 01 Dec 2019 11:39:22 -0500
parents a19c9e1fd077
children 5b5c61ad8d2b
(defpackage :advent/2018/10 #.cl-user::*advent-use*)
(in-package :advent/2018/10)

(defun parse-line (line)
  (destructuring-bind (x y vx vy) line
    (cons (complex x y)
          (complex vx vy))))

(defun x (star)
  (realpart (car star)))

(defun y (star)
  (imagpart (car star)))

(defun tick (stars)
  (dolist (star stars)
    (incf (car star) (cdr star))))

(defun bounds (stars)
  (values (x (extremum stars '< :key #'x)) ; left
          (x (extremum stars '> :key #'x)) ; right
          (y (extremum stars '< :key #'y)) ; bottom
          (y (extremum stars '> :key #'y)))) ; top

(defun field-size (stars)
  (multiple-value-bind (left right bottom top)
      (bounds stars)
    (* (- right left) (- top bottom))))

(defun draw (stars)
  (multiple-value-bind (left right bottom top) (bounds stars)
    (let* ((height (1+ (- top bottom)))
           (width (1+ (- right left)))
           (field (make-array height)))
      (do-array (line field)
        (setf line (make-string width :initial-element #\space)))
      (dolist (star stars)
        (setf (aref (aref field (- (y star) bottom))
                    (- (x star) left))
              #\*))
      (map nil #'write-line field))))

(define-problem (2018 10) (data read-lines-of-numbers-and-garbage)
  (iterate
    (with stars = (mapcar #'parse-line data))
    (with ticks = 0)
    (initially (iterate
                 (tick stars)
                 (incf ticks)
                 (until (< (field-size stars) 3000))))
    (format t "After tick ~D:~%" ticks)
    (draw stars)
    (until (string= "q" (read-line)))
    (tick stars)))

(1am:test test-2018/10
  ;; This can't really be tested automatically :(
  (values))