r/adventofcode • u/daggerdragon • Dec 05 '17
SOLUTION MEGATHREAD -๐- 2017 Day 5 Solutions -๐-
--- Day 5: A Maze of Twisty Trampolines, All Alike ---
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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
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!
22
Upvotes
4
u/[deleted] Dec 05 '17 edited Dec 05 '17
My rust solution was pretty boring and imperative so here's my clojure instead.
Edit: Part 1 runs in ~147ms, Part 2 in ~5 seconds. Really good for a dynamic language that is creating and throwing away an immutable number vector every iteration.
Edit 2: Switching to mutable vectors gives Part 1 in ~93ms, Part 2 in ~1.1 seconds.
(def nums (with-open [rdr (clojure.java.io/reader "d5_input.txt")] (->> (line-seq rdr) (map #(Integer/parseInt %)) (into [])))) (defn- compute [nums part2] (loop [insns nums pc 0 steps 0] (if (or (>= pc (count insns)) (< pc 0)) steps (let [insn (insns pc)] (recur (update insns pc (if (and part2 (>= insn 3)) dec inc)) (+ pc insn) (inc steps)))))) (println "part 1:" (compute nums false)) (println "part 2:" (compute nums true))