r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-

--- Day 6: Chronal Coordinates ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:26:52!

32 Upvotes

387 comments sorted by

View all comments

1

u/[deleted] Dec 13 '18

Common lisp:

(defstruct coord x y)

(defun read-input ()
  (with-open-file (in "06.input")
    (loop for line = (read-line in nil)
          while line collect (with-input-from-string (in (remove #\, line))
                               (make-coord :x (read in) :y (read in))))))

(defun dist (c x y)
  (+ (abs (- (coord-x c) x))
     (abs (- (coord-y c) y))))

(defun bounding-box (coords)
  (loop for c in coords
        minimize (coord-y c) into top
        maximize (coord-y c) into bottom
        minimize (coord-x c) into left
        maximize (coord-x c) into right
        finally (return (list top bottom left right))))

(defun nearest-coord (coords x y)
  (loop with min-dist = most-positive-fixnum
        with min-coord
        for coord in coords
        for dist = (dist coord x y)
        if (< dist min-dist) do
          (setf min-dist dist
                min-coord coord)
        else if (= dist min-dist) do
          (setf min-coord nil)
        finally (return min-coord)))

(defun largest-area (coords bbox)
  (let ((areas (make-hash-table)))
    (destructuring-bind (top bottom left right) bbox
      (loop for x from left below right do
        (loop for y from top below bottom
              for nearest = (nearest-coord coords x y) do
                (if (or (= left x) (= right x) (= top y) (= bottom y))
                    (setf (gethash nearest areas) most-negative-fixnum)
                    (incf (gethash nearest areas 0))))))
    (loop for val being the hash-values of areas using (hash-key key)
          when key maximize val)))

(defun region-size (coords bbox)
  (let ((count 0))
    (destructuring-bind (top bottom left right) bbox
      (loop for x from left below right do
        (loop for y from top below bottom
              for sum = (reduce #'+ coords :key (lambda (c) (dist c x y)))
              when (< sum 10000) do (incf count))))
    count))

(defun main ()
  (let* ((coords (read-input))
         (bbox (bounding-box coords)))
    (format t "Result 6a: ~d~%" (largest-area coords bbox))
    (format t "Result 6b: ~d~%" (region-size coords bbox))))

;; CL-USER> (time (main))
;; Result 6a: 3871
;; Result 6b: 44667
;; Evaluation took:
;;   0.683 seconds of real time
;;   0.678813 seconds of total run time (0.678813 user, 0.000000 system)
;;   99.41% CPU
;;   1,478,213,022 processor cycles
;;   2,980,912 bytes consed