Skip to content

Instantly share code, notes, and snippets.

@honzabrecka
Forked from kepi74/AoC2018-day-01.clj
Last active December 2, 2018 14:43
Show Gist options
  • Save honzabrecka/8bc8c97a7d049f361e6c5417a0d70038 to your computer and use it in GitHub Desktop.
Save honzabrecka/8bc8c97a7d049f361e6c5417a0d70038 to your computer and use it in GitHub Desktop.
Advent of Code 2018 day 01
(defn get-final-freq
"Day one: calculate resulting frequency"
[freq_list]
; 1. clojurist prefer to use `-` instead of `_` in names
; 2. I'm not sure how you got and what's inside `freq_list` (I suppose integers, then it's fine)
(reduce + freq_list))
(defn repeated-freq
"Day one: first occur of repeated frequency"
[freq_list]
(reduce
(fn [vect freq]
; 3. I don't like the name `vect` (clojurist would use `col`), but in this context it's
; something like `seen`
(let [curr_freq (+ (last vect) freq)
new_vect (conj vect curr_freq)
; 4. I don't like the name `new_vect`, for me it's `frequencies` (BTW take a look at
; reductions function in core - does the same thing but lazily)
]
(if (some #(= curr_freq %) vect)
; 5. This is the place where I would start to think about better data structure,
; overall idea is good, but the complexity of this algo is not - could be
; better if you used set
(reduced (last new_vect))
; 6. Two times calling `last` on vector has speed consequencies (small detail, I know, who cares)
new_vect)))
[0]
(cycle freq_list)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment