Day 2: Fulminating Farmland
We’re on a farmland and there’s a robot working on it. It follows six colorful waypoints depending on its program.
(def ^:const waypoints "roygbv")(def ^:const wp-len (count waypoints))The program is our puzzle’s input. For example:
(def example "obbvb")Input parsing
We could work with strings and characters without any special input parsing, but let’s convert the characters to their index in waypoints. Since there are only 6 waypoints, we could do it manually, but there’s a neat trick with zipmap function so let’s use that.
(def wp-indices (zipmap waypoints (range)))wp-indices{\r 0, \o 1, \y 2, \g 3, \b 4, \v 5}Looks good. Time to do that for the whole input:
(defn parse-data [input]
(mapv wp-indices input))(def example-data (parse-data example))(def data (parse-data (aoc/read-input 2)))example-data[1 4 4 5 4]Part 1
In Part 1 we need to find a total distance the robot will travel based on the program.
(defn part-1 [data]
(let [len (count data)]
(loop [i 0
idx 0
dist 0]
(if (= i len)
dist
(let [idx' (long (nth data i))
d (- idx' idx) ; [1]
d' (cond-> d
(<= d 0) (+ wp-len))] ; [2]
(recur (inc i) idx' (+ dist d')))))))For each step we need to calculate the distance d between the current waypoint (idx') and the previous waypoint (idx). [1]
If the calculated distance is zero or negative, to get the correct distance we need to add the total length of the waypoints to it. [2]
(part-1 example-data)16(part-1 data)3228Part 2
Initial solution
In Part 2 we need to rearrange our input to make the robot travel the furthest.
We still start from the red (r) waypoint, and the optimal way is to visit all red waypoints first, then to visit all v waypoints (they have the highest index), then b, g, y, o, i.e. to visit waypoints in reverse.
Two things to be aware of:
if there are
nwaypoints of some color, we first spend some distancelto get to it, and then there aren-1waypoints of that color with distance6some waypoint colors might be missing, so we cannot blindly set
lto always be5
To get the number of each color in our input, we’ll use our friend from yesterday: frequencies.
(defn part-2 [data]
(let [freqs (frequencies data)]
(loop [i (dec wp-len) ; [1]
idx wp-len
dist (* wp-len (freqs 0))] ; [2]
(if (zero? i) ; [3]
dist
(if-let [n (freqs i)] ; [4]
(let [d (- (* wp-len n) (- idx i))] ; [5]
(recur (dec i) i (+ dist d)))
(recur (dec i) idx dist)))))) ; [6]Since we start on it, we can immediately calculate the distance after visiting all r waypoints [2], and then we’ll visit other waypoints in reverse [1].
If we came back to r (index 0), we’re done and return the total distance. [3]
Otherwise, we check if there are any waypoints of index i in our program and how many (n). [4] If they aren’t any, we move to the next index while keeping the old index idx as it was. [6] If there are, we add their distances to the accumulator and continue with the next step. Notice that in [5] we are subtracting the difference between the previous and current index from the product to get the correct distance.
Part 2 uses different examples, so let’s check if we’re getting the expected answer for one of them:
(def ex2-data (parse-data "roygbvroygbvroygbv"))(part-2 ex2-data)103Correct. Time to do it on our input:
(part-2 data)5443Simpler solution
We got a correct answer, but: is there a simpler way we could do this?
If we look at the line [5] from above, we can notice we’re doing the same thing for each waypoint: calculating (* wp-len n) and then subtracting some difference.
After I sketched on paper several examples, I noticed that the total of these differences is always the same: (dec wp-len). (Note: for some inputs, not for all, e.g. if they are missing o, this wouldn’t be correct.)
This means that we can just sum all the counts and subtract that total difference.
Also, since we’re doing (* wp-len n) for each n and we’re adding those, we can first add all ns and then multiply them at the end: (+ (* x 3) (* x 4)) is the same as (* x (+ 3 4)).
And adding all ns gives us…. the length of the input!
(defn part-2-simpler [data]
(- (* wp-len (count data))
(dec wp-len)))(part-2-simpler ex2-data)103(part-2-simpler data)5443Still correct and much simpler.
Conclusion
Unlike Day 1, this one should allow for many different approaches. Here are two of them.
Today’s highlights:
zipmap: create a hashmap from keys in the first collection to values in the second collection
source: clojure/src/day02.clj