Advent of Code 2022 - Day 06
Day 06
This challenge invloved getting every possible sequence of four adjacent characters and seeing if the characters are distinct. This invloved creating a recursive function because there was no built in function to do the task.
Part 1
We introduce a variable j which starts at 4 and then take a substring of the main string from j - 4 to j to
get the four adjacent characters. We then expland this structure with apply and check if all the elements are
distint with distinct?. If they are we found the position, and if not we recur with incresed j.
1(defn part1
2 [s]
3 (loop [j 4]
4 (let [packet (subs s (- j 4) j)]
5 (if (apply distinct? packet)
6 j
7 (recur (inc j))))))
Part 2
For this part we just need to change the number of adjacent character from 4 to 14.
1(defn part2
2 [s]
3 (loop [j 14]
4 (let [packet (subs s (- j 14) j)]
5 (if (apply distinct? packet)
6 j
7 (recur (inc j))))))