DEV Community

AN
AN

Posted on

Advent of Code 2015 (day 2)

I expect the heat death of the universe before this series is completed :|

Previously

Day 2

Part 1

There are no tricks here - we just have to take a list of package dimensions and determine how much wrapping paper we need to wrap each of them according to a formula we're given.

; We're using a regex to get the digits out of our
; dimension property e.g. "1x2x3" -> ["1x2x3" "1" "2" "3"]
(def re #"(\d+)x(\d+)x(\d+)")

(defn dimensions [str]
  (let [match (re-find re str)]
    (if (nil? match) nil
        (->> (rest match)
             (map #(Integer/parseInt %1))
             (sort)))))

(def input (->> (slurp "input")
                (str/split-lines)
                (map dimensions)
                (filter (complement nil?))))
Enter fullscreen mode Exit fullscreen mode

We're defining a variable "input" which is taken from slurping a file with the puzzle input. We then use our dimensions function to turn each text row into a list of 3 numbers: our dimensions.

Half the effort on these early puzzles is just transforming the input into something usable :)

Note the sort in our dimensions function. This means that our resulting dimensions are in the order from smallest to largest. Our puzzle requires us knowing the shortest sides so we know the first two entries in each list are those.

Finally we just filter to remove any items which didn't match.

(defn area [[w h l]]
  (+ (* 2 l w)
     (* 2 w h)
     (* 2 h l)
     (* w h)))

(def part-1-answer (apply + (map area input)))
Enter fullscreen mode Exit fullscreen mode

The answer is just a sum of the areas of each parcel size we're given.

Part 2

The second part required working out how much ribbon was required for each parcel and figuring out the total length of ribbon required.

The solution is pretty much a repeat of the last answer. I defined a new function ribbon-length which took parcel dimensions as an argument. Then it's just mapping it to each input and returning the sum.

Closing Thoughts

  • This was a straightforward puzzle.
  • Hooray for destructuring arguments.
  • I used Babashka for this day. It's definitely faster to startup and the integration with Calva was great.

Top comments (0)