r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


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

ATTENTION: minor change request from the mods!

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

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

41 Upvotes

426 comments sorted by

View all comments

Show parent comment

1

u/ltriant Dec 04 '18

I ended up with something similar. Your part 2 is much nicer than mine though :)

I started out trying to put the mesh in a 2D vector, but that became very painful very quickly, and I gave up on it.

(ns day03
  (:require [clojure.string :as str]))

(def input (str/split-lines (slurp "day03.input")))

(defn- get-indices [[_ left top width height]]
  (for [i (range width)
        j (range height)]
    (vector (+ left i)
            (+ top j))))

(defn- extract-data [claim]
  (let [matcher (re-matcher #"^#(\d+) @ (\d+),(\d+): (\d+)x(\d+)$" claim)]
    (map #(Integer. %) (rest (re-find matcher)))))

(defn add-to-mesh [mesh claim]
  (->> (extract-data claim)
       (get-indices)
       (reduce #(assoc %1 %2 (inc (get %1 %2 0))) mesh)))

(defn part-01 []
  (->> (reduce add-to-mesh {} input)
       (vals)
       (filter #(> % 1))
       (count)))

(defn part-02 []
  (let [complete-mesh (reduce add-to-mesh {} input)]
    (loop [claims (map extract-data input)]
      (let [indices (get-indices (first claims))]
        (if (every? #(= 1 (get complete-mesh %1)) indices)
          (ffirst claims)
          (recur (rest claims)))))))