Day 8 - Solutions
I guess that wouldnβt have been solvable without proper data inspection π
here's my solution: https://github.com/rmrt1n/advent-of-code-2023-clj/blob/main/src/aoc/day08.clj
https://gitlab.com/maximoburrito/advent2023/-/blob/main/src/day08/main.clj After getting tricked by not trying the brute force approach, this time I wasted time on a brute force solution. When it was clear it wouldn't work, I wrote some code to explore the data a bit. Thankfully all the cycles repeated exactly, so it it was easy to calculate the solution and math where they all meet.
solution: https://github.com/erdos/advent-of-code/blob/master/2023/day08.clj
went with counting reductions over the cycle of the instructions and using reduced to shortcut.
https://gist.github.com/andrewbelo/7d0ae27b04da17da2b44081fc81f985c
Pretty much the same solution, using reduce over the cycle
I just did day 8 In Clojure, having previously done it Kotlin. I'm pretty proud of how much more succinct my Clojure solution (very functional) is than my Kotlin one (which is a bit more OO). https://github.com/cdpjenkins/advent-of-code/blob/main/2023/clj-aoc-2023/src/clj_aoc_2023/day08.clj
Thank you norman for the hint. https://github.com/arnaudgeiser/advent-of-code/blob/master/2023/clojure/src/advent_of_code/day8.clj
https://github.com/skazhy/advent/blob/master/src/advent/2023/day8.clj nice and easy π
(Was lazy and added numeric-tower to deps...)
wasted quite some time on the brute force approach for part2 before I actually looked at the size of the problem space and noped out. i reached for lcm because there's always a gcd/lcm puzzle in AOC and all the emphasis on "simultaneously" walking part2 made me think I probably shouldn't actually do that. Why LCM works though is pretty untuitive for me. https://github.com/Ramblurr/advent-of-code/blob/main/src/aoc/2023/day08.clj
cycle, reductions, take-while and lcm https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2023/day08.clj
iterate and take-upto , no reduce.
https://github.com/tschady/advent-of-code/blob/main/src/aoc/2023/d08.clj
My https://github.com/wevre/advent-of-code/blob/65f001fbb8967792e373da0b12f753a012819903/notes/notes-aoc2023.txt#L192 and my https://github.com/wevre/advent-of-code/blob/master/src/advent_of_code/2023/day_08.clj.
Second part is waaay goood! I enjoyed and feel fun!
(defn f [i n]
(cond
(> (* i i) n) [n]
(zero? (mod n i)) (conj (f i (quot n i)) i)
:else (f (inc i) n)))
(let [[path m] (str/split input #"\n\n" 2)
m (->> (str/split m #"\n")
(keep (fn [s] (-> s
(str/replace #"[A-Z0-9]+" #(str "\"" % "\""))
(str/split #"\=" 2)
(->> (mapv read-string)))))
(into {}))
lr (fn [i p] (let [[l r] (get m p)
i (mod i (count path))]
(if (= \L (get path i)) l r)))
g (fn [p-start]
(loop [p p-start
i 0]
(let [p (lr i p)]
(if (= \Z (get p 2))
(inc i)
(recur p (inc i))))))]
(->> m
keys
(filter #(= \A (get % 2)))
(map (comp frequencies (partial f 2) g))
(apply merge-with max)
(mapcat identity)
(apply *)))https://github.com/rjray/advent-2023-clojure/blob/master/src/advent_of_code/day08.clj
I also (briefly) tried to brute-force part 2, and also figured out the cycles. Used clojure.math.numeric-tower/lcm for the math.
Lcm is easy to implement - look at f (efficient fast factorization, although it may be even more faster - by stepping in 2 or by prime numbers) in my code above, and last 3 strings - for combining factors in lcm
https://github.com/samcf/advent-of-code/blob/main/2023-08-haunted-wasteland.clj Finding where sequences converge is an aoc classic and I always trip up on it
Day 8 part 2 almost tricked me into implementing LCM. But then I looked closer at the inputs. https://github.com/bhauman/adv2023/blob/main/src/adv2023/day08/sol.clj
So the length of the directions string βLRLR β¦β is most likely a prime factor, because all the paths contain it. If you divide all the cycle-lengths (distance to first Z ending location) of the different paths followed by that factor you may end up with a set of unique prime factors, so the ultimate number of steps is (* dir-length (/ cycle-length-1 dir-length) ...
how did I not remember clojure.math.numeric-tower?
@bhauman I did end up dividing each steps count by my directions count and thought something fun could be done with that idea but wasn't quite sure so I abandoned it
Nice solution!
Thanks! but now I wish Iβd done the numeric-tower/lcm route. It would have made my second answer very brief.
Iβm really enjoying reading everyoneβs solutions. This is my kind of fun. π
really happy with mine! https://github.com/FelipeCortez/advent-of-code/blob/master/2023/08.clj
Nice solution @felipecortezfi!
mapcat would be a good candidate on line 11:
;;(into {} (comp (map parse-mapping) cat) mappings)
VS
(into {} (mapcat parse-mapping) mappings)mapcat isnβt a transducer though?
My version of day 8 part 1 : https://github.com/prestancedesign/advent-of-babashka-template/blob/main/src/aoc23/day08.cljc
Not sure about transduser (need chek at clojredocs), but it is definitelly List monads bind, so that is why it widelly used in algorythms based on lists π
oh wait mapcat is a transducer itβs definition is (comp (map f) cat)
This gave me a chuckle.
I got that. The odd thing is that if you ask it to translate the input to English, it does change it
Yeah, it translates to, "HELP! I'M TRAPPED INSIDE A GRAPH TRAVERSAL PROBLEM!"