Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Advent of Code - 2018 - Day 1
(ns aoc1)
(defn calibrate-1
"Simple solution for part 1 of AoC 2018 Day 1"
(let [raw-input (slurp input-file)
freqs (map #(Integer. %) (clojure.string/split-lines raw-input))]
(reduce + freqs)))
;; Below contents are from part 2, which didn't work well.
(defn frequency-adjustments [freqs]
(reductions + (cycle freqs)))
(defn calibrate [freqs]
(loop [n 2]
(let [steps (take n (frequency-adjustments freqs))
step-counts (frequencies (rest steps))
repeats (filter (fn [[k v]] (if (= v 2) k)) step-counts)]
(if-not (empty? repeats)
(first (keys repeats))
(recur (inc n))))))
;; Part 2.5, which was co-opted from
(defn calibrate2 [freqs]
(loop [xs (cycle freqs) seen #{} total 0]
(if (contains? seen total)
(recur (rest xs) (conj seen total) (+ total (first xs))))));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.