aoc2018/clojure/src/aoc2018/day01.clj -rw-r--r-- 739 bytes View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(ns aoc2018.day01
  (:require [clojure.java.io :as io]
            [clojure.string :refer [split-lines]]))

(def input-file (-> "day01.in" io/resource io/file))

(defn parse-input [input]
  (->> input split-lines (map read-string)))

(defn part1 [diffs]
  (reduce + diffs))

(defn first-duplicate [freqs]
  (loop [freqs freqs
         visited #{}]
    (let [freq (first freqs)]
      (if (contains? visited freq)
        freq
        (recur
          (rest freqs)
          (conj visited freq))))))

(defn part2 [diffs]
  (->> diffs
    cycle
    (reductions +)
    first-duplicate))

(defn main []
  (let [diffs (parse-input (slurp input-file))]
    (println "Part 1:" (time (part1 diffs)))
    (println "Part 2:" (time (part2 diffs)))))