Skip to content

Commit

Permalink
Day 10
Browse files Browse the repository at this point in the history
Added solutions for day10. Also added function "build-set" to utilities.lisp.
  • Loading branch information
abraemer committed Dec 10, 2018
1 parent 800864b commit 1d536e6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion advent-of-code-2018.asd
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
(:file "day6")
(:file "day7")
(:file "day8")
(:file "day9")))
(:file "day9")
(:file "day10")))
45 changes: 45 additions & 0 deletions day10.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
;;;; day10.lisp

(in-package :advent-of-code-2018)

(defun day10-parse-input ()
(let ((input
(loop-line-by-line (puzzlepath "input10.txt")
:collect (ppcre:register-groups-bind ((#'parse-integer x y dx dy))
("position=<( ?-?\\d+), ( ?-?\\d+)> velocity=<( ?-?\\d+), ( ?-?\\d+)>" line)
(list x y dx dy)))))
(make-array (length input) :element-type 'list :initial-contents input)))

(defun day10-step (points &optional (amount 1))
(loop :for point :across points
:do (incf (first point) (* (third point) amount))
:do (incf (second point) (* (fourth point) amount)))
points)

(defun day10-boundary (points)
(loop :for (x y) :across points
:minimize x :into min-x
:maximize x :into max-x
:minimize y :into min-y
:maximize y :into max-y
:finally (return (list min-x min-y (- max-x min-x) (- max-y min-y)))))

(defun day10-pretty-print (points)
(destructuring-bind (dx dy width height) (day10-boundary points)
(loop
:with positions := (build-set points :test 'equal :key (lambda (p) (cons (first p) (second p))))
:for y :upto height
:do (loop :for x :upto width
:do (format t (if (gethash (cons (+ dx x) (+ dy y)) positions) "#" " ")))
:do (format t "~%"))))

(defun day10 ()
(loop :for points := (day10-parse-input) :then (day10-step points)
:for (nil nil width height) := (day10-boundary points)
:for last-area := nil :then area
:for area := (* width height)
:until (and last-area (> area last-area))
:count t :into seconds
:finally (progn
(format t "After ~a seconds the following message appears:~%~%" (1- seconds))
(day10-pretty-print (day10-step points -1)))))
5 changes: 5 additions & 0 deletions utilities.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@
:for value := (aref vector index)
:when (> value max-val) :do (setf max-ind index max-val value)
:finally (return (values max-ind max-val))))

(defun build-set (seq &key (test 'eql) (key #'identity))
(let ((set (make-hash-table :test test)))
(map nil (lambda (k) (setf (gethash (funcall key k) set) t)) seq)
set))

0 comments on commit 1d536e6

Please sign in to comment.