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/phil_g Dec 06 '18

No Common Lisp yet?

Okay:

(defparameter *targets* (mapcar (lambda (s) (apply #'complex (extract-ints s)))
                                (input)))

;; Just shorthand so I have to type less.
;; Also, WARNING: SBCL-specific
(defparameter *infinity* sb-ext:double-float-positive-infinity)

(defun bounding-box (targets)
  (iter (for target in targets)
        (minimizing (realpart target) into minx)
        (minimizing (imagpart target) into miny)
        (maximizing (realpart target) into maxx)
        (maximizing (imagpart target) into maxy)
        (finally (return (list minx miny maxx maxy)))))

(defun manhattan-distance (c1 c2)
  (+ (abs (- (realpart c1) (realpart c2)))
     (abs (- (imagpart c1) (imagpart c2)))))

(defun closest-target (c targets)
  (iter (for target in targets)
        (finding target minimizing (manhattan-distance c target))))

(defun largest-finite-area (targets)
  (destructuring-bind (minx miny maxx maxy)
      (bounding-box targets)
    (let ((areas (make-hash-table)))
      (iter (for target in targets)
            (setf (gethash target areas) 0))
      (iter (for x from minx to maxx)
            (iter (for y from miny to maxy)
                  (for target = (closest-target (complex x y) targets))
                  (if (or (= x minx)
                          (= x maxx)
                          (= y miny)
                          (= y maxy))
                      (setf (gethash target areas) *infinity*)
                      (incf (gethash target areas)))))
      (values-list (iter (for (target area) in-hashtable areas)
                         (when (/= area *infinity*)
                           (finding (list area target) maximizing area)))))))

(defun get-answer-1 ()
  (largest-finite-area *targets*))


(defun total-distance (c targets)
  (iter (for target in targets)
        (summing (manhattan-distance c target))))

(defun count-central-coordinates (threshold targets)
  (destructuring-bind (minx miny maxx maxy)
      (bounding-box targets)
    (iter outer
          (for x from minx to maxx)
          (iter (for y from miny to maxy)
                (in outer
                    (counting (< (total-distance (complex x y) targets)
                                 threshold)))))))

(defun get-answer-2 ()
  (count-central-coordinates 10000 *targets*))

Full code at https://gitlab.com/asciiphil/advent-of-code/blob/master/2018/06.lisp .

I also made some visualizations of my data:

2

u/rabuf Dec 07 '18

How does your closest-target ensure that a point shared by two or more won't get assigned to one of them arbitrarily?

1

u/phil_g Dec 07 '18

It, uh, doesn't. I got lucky with my dataset and didn't realize there was a bug until I redid the logic in Python for the visualizations.

I should probably go back and fix it or something. 😕

1

u/rabuf Dec 07 '18

Yeah, that's what I was thinking. I liked using iterate to generate the list, but I captured the whole list and then sorted, then took the top of the sorted list or nil if the top 2 matched each other:

(defun closest-point (coordinate points)
    (let ((sorted (sort (iter (for p in points)
                              (collecting (list p (manhattan-distance coordinate p))))
                        #'<
                        :key #'cadr)))
        (if (= (cadr (car sorted))
               (cadr (cadr sorted)))
            nil
            (caar sorted))))

Then I used a cond and if the result of closest-point was null I just did nothing.

I had a bug in my code for the longest time because I had forgotten sort was destructive and was sorting points directly based on the Manhattan distance. That created some fun glitches in my program.

2

u/rabuf Dec 07 '18

It's not precisely true, but if you used negative-infinity instead of positive, you can remove that when clause. The negative infinity areas can't be the max unless all areas are infinite.

1

u/phil_g Dec 07 '18

Oh, yeah. I didn't think of that, but it would have simplified the logic. I was just being literal. 🙂

Good catch!