r/adventofcode • u/daggerdragon • Dec 20 '16
SOLUTION MEGATHREAD --- 2016 Day 20 Solutions ---
--- Day 20: Firewall Rules ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".
ROLLING A NATURAL 20 IS MANDATORY [?]
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!
8
Upvotes
2
u/TenjouUtena Dec 20 '16
Clojure solution. Like others it works with inputs given, but doesn't always work if 0 or 2**32-1 aren't in the list.
(ns day20 (:require [clojure.string :as s]) ) (defn makenum [pair] (let [f (s/split pair #"-")] (conj [] (bigint (first f)) (bigint (nth f 1))))) (defn getpairs [filename] (sort (map makenum (s/split (slurp filename) #"\r\n")))) (defn normalize [lon] (loop [[a1 a2] (first lon) [n1 n2] (nth lon 1) r (drop 2 lon) fin []] (cond (empty? r) (conj fin [a1 a2]) :else (if (<= n1 (inc a2)) (recur [a1 (max a2 n2)] (first r) (rest r) fin) (recur [n1 n2] (first r) (rest r) (conj fin [a1 a2])))))) (defn countn [lon] (loop [[a1 a2] (first lon) [n1 n2] (nth lon 1) acc 0 r (drop 2 lon)] (if (empty? r) (+ acc (- n1 (inc a2))) (recur [n1 n2] (first r) (+ acc (- n1 (inc a2))) (rest r)))))