Remix of

# Advent of Code - Day 1

To get going we will simply start with the latest clojure version...

{:deps
{org.clojure/clojure {:mvn/version "1.10.0-RC2"}}}
deps.edn
Extensible Data Notation

The first 2 goals rely on a an input file that is likely unique to each user. I've simply saved my input to a text file and mounted it in NextJournal as seen here.

frequency_input.txt

## 1. Part One

The data supplied should be imported as a list of positive and negative numbers. Clojure makes this simple, as we can simply utilize read-string to convert all the strings to the appropriate positive or negative number like so.

(require '[clojure.string :as str])

(def input-data
(->> "frequency_input.txt"
slurp
str/split-lines
(map read-string)))

To see that the conversion was done as we expect we can take a look at the first 10 values.

(take 10 input-data)

The goal of the first step is to simply, starting from 0, find the resulting frequency by adding each number provided from the input. Utilizing reduce we can easily determine the result.

(reduce + input-data)

## 2. Part Two

The next part is slightly more complex. The goal is that after each summation we need to determine if we've seen that sum before. If so, then return that value. Otherwise keep processing the input repeatedly until a given frequency is repeated.

To accomplish this, I utilized loop and recur and maintain a set of seen frequencies. I process each frequency and check if it is contained in my set of seen frequencies. If it has not been seen then we recur with the rest of the input, the current frequency, and the new set of seen items that contains the current frequency. For this to work we need our input data to be repeated and this can be accomplished by using the cycle function.

(loop [input (cycle input-data)
frequency 0
seen #{}]
(let [frequency (+ frequency (first input))]
(if (contains? seen frequency)
frequency
(recur (rest input) frequency (conj seen frequency)))))

And that's it to the first day of Advent of Code 2018!