r/adventofcode • • Dec 02 '15

Spoilers Day 2 solutions

Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.

15 Upvotes

161 comments sorted by

View all comments

Show parent comment

2

u/SimonS Dec 02 '15

I also went Clojure. I'm very new, so dunno how idiomatic it is - it's fun writing it though :D

Solutions to this and any future ones I do at https://github.com/SimonS/adventofcode-answers/ - dunno if I'll do all 24 in Clojure, it's quite the departure for me.

I don't like that the total-data function processes the lists twice, but it reads nicer than the version compounded into one reduce call.

(def normalised-data
  (->> (slurp "answers/inputs/day02.txt")
       clojure.string/split-lines
       (map #(map read-string (clojure.string/split % #"x")))))

(defn paper [[l w h]] (let [sides [(* l w) (* w h) (* h l)]]
                      (+ (* 2 (reduce + sides))
                      (apply min sides))))

(defn ribbon [dimensions]
  (let [surface-area (apply * dimensions)
        perimeter (->> (sort dimensions)
                       (take 2)
                       (map (partial * 2))
                       (reduce +))]
    (+ surface-area perimeter)))

(defn total-data [func]
  (->> (map func normalised-data)
       (reduce +)))

;; Part 1
(total-data ribbon)

;; Part 2
(total-data paper)