Skip to content

Instantly share code, notes, and snippets.

@ballpointcarrot
Created December 2, 2018 23:45
Show Gist options
  • Save ballpointcarrot/a8f5339a788b8ac5c78312d01a2a3d04 to your computer and use it in GitHub Desktop.
Save ballpointcarrot/a8f5339a788b8ac5c78312d01a2a3d04 to your computer and use it in GitHub Desktop.
Advent of Code - 2018 - Day 1
(ns aoc1)
(defn calibrate-1
"Simple solution for part 1 of AoC 2018 Day 1"
[input-file]
(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 https://www.reddit.com/r/adventofcode/comments/a20646/2018_day_1_solutions/eau9gjm/
(defn calibrate2 [freqs]
(loop [xs (cycle freqs) seen #{} total 0]
(if (contains? seen total)
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