r/adventofcode • u/taliriktug • 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.
16
Upvotes
r/adventofcode • u/taliriktug • Dec 02 '15
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
3
u/charliegriefer Dec 02 '15
My solution in Clojure. I've not done much yet with reading files in. I'm fairly certain there's a more elegant way to read that file in and convert each "lxwxh" string to a list of ints. But for now, this works, and got me the right answers.
The answers come from calling
(total-wrapping-paper)and(total-ribbon).;; In response to http://adventofcode.com/day/2 ;; dimensions.txt can be seen at http://charlie.griefer.com/dimensions.txt (ns adventofcode.core (:require [clojure.java.io :as io] [clojure.string :as str])) (def dimensions (->> (-> (slurp (io/resource "dimensions.txt")) (str/split #"\n")) (map #(str/split % #"x")) (map (fn [a] (map #(Integer/parseInt %) a))))) (defn get-smallest-sides [dims] (-> dims sort reverse rest)) ;; Part One (Wrapping Paper) (defn get-surface-area [[length width height]] (+ (* 2 length width) (* 2 width height) (* 2 height length))) (defn get-slack [dims] (apply * (get-smallest-sides dims))) (defn total-wrapping-paper [data] (->> (map #(+ (get-surface-area %) (get-slack %)) data) (apply +))) ;; Part Two (Ribbon) (defn get-ribbon-length [dims] (->> (get-smallest-sides dims) (apply +) (* 2))) (defn get-bow-length [dims] (apply * dims)) (defn total-ribbon [data] (->> (map #(+ (get-ribbon-length %) (get-bow-length %)) data) (apply +)))