Skip to content

Instantly share code, notes, and snippets.

@commander-trashdin
Last active December 5, 2021 15:08
Show Gist options
  • Save commander-trashdin/036149ddc40a4825aacc66eeb568bb92 to your computer and use it in GitHub Desktop.
Save commander-trashdin/036149ddc40a4825aacc66eeb568bb92 to your computer and use it in GitHub Desktop.
AoC day 5, stupid solution
(defun read-coords (path)
(with-open-file (stream path :direction :input :if-does-not-exist :error)
(let ((res (make-array 0 :adjustable t :fill-pointer 0)))
(loop :for (x1 y1 _ x2 y2)
:= (uiop:split-string (read-line stream nil nil) :separator ", ")
:while x1
:do (vector-push-extend (list (parse-integer x1)
(parse-integer y1)
(parse-integer x2)
(parse-integer y2)) res))
res)))
(defun max-coord (array)
(loop :for (x1 y1 x2 y2) :across array
:maximize (max x1 y1 x2 y2)))
(defun day5.1 (path)
(let* ((coords (read-coords path))
(max (max-coord coords))
(table (make-array (list (+ 1 max) (+ 1 max)) :element-type 'fixnum)))
(loop :for (x1 y1 x2 y2) :across coords
:when (= x1 x2)
:do (loop :for i :from (min y1 y2) :to (max y1 y2)
:do (incf (aref table i x1)))
:when (= y1 y2)
:do (loop :for i :from (min x1 x2) :to (max x1 x2)
:do (incf (aref table y1 i))))
(loop :for i :from 0 :to max
:sum (loop :for j :from 0 :to max
:count (> (aref table i j) 1)))))
(defun day5.2 (path)
(let* ((coords (read-coords path))
(max (max-coord coords))
(table (make-array (list (+ 1 max) (+ 1 max)) :element-type 'fixnum)))
(loop :for (x1 y1 x2 y2) :across coords
:when (= x1 x2)
:do (loop :for i :from (min y1 y2) :to (max y1 y2)
:do (incf (aref table i x1)))
:when (= y1 y2)
:do (loop :for i :from (min x1 x2) :to (max x1 x2)
:do (incf (aref table y1 i)))
:when (= (abs (- x1 x2)) (abs (- y1 y2)))
:do
(do ((x x1 (if (> x1 x2) (- x 1) (+ x 1)))
(y y1 (if (> y1 y2) (- y 1) (+ y 1))))
((= x x2))
(incf (aref table y x)))
(incf (aref table y2 x2)))
(loop :for i :from 0 :to max
:sum (loop :for j :from 0 :to max
:count (> (aref table i j) 1)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment