Created
December 2, 2018 23:45
-
-
Save ballpointcarrot/a8f5339a788b8ac5c78312d01a2a3d04 to your computer and use it in GitHub Desktop.
Advent of Code - 2018 - Day 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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