-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.clj
38 lines (32 loc) · 1.53 KB
/
day08.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(ns advent-2023-clojure.day08
(:require [clojure.string :as str]
[abyala.advent-utils-clojure.core :refer :all]
[clojure.math.numeric-tower :as math]))
(defn parse-input [input]
(let [[instructions _ & mappings] (str/split-lines input)]
{:instructions instructions
:paths (reduce (fn [acc line]
(let [[src dest1 dest2] (re-seq #"\w{3}" line)]
(assoc acc [src \L] dest1 [src \R] dest2)))
{}
mappings)}))
(defn all-steps [instructions paths start-loc]
(letfn [(next-step [loc turns] (let [[turn & turns'] turns
loc' (paths [loc turn])]
(cons loc (lazy-seq (next-step loc' turns')))))]
(next-step start-loc (cycle instructions))))
(defn steps-to-end [end-loc? instructions paths start-loc]
(index-of-first #(end-loc? %)
(all-steps instructions paths start-loc)))
(defn solve [start-locs-fn end-loc? input]
(let [{:keys [instructions paths]} (parse-input input)]
(reduce math/lcm (map #(steps-to-end end-loc? instructions paths %)
(start-locs-fn paths)))))
(defn single-start [_] ["AAA"])
(defn multi-start [paths] (->> (map ffirst paths)
(filter #(str/ends-with? % "A"))
set))
(defn part1 [input]
(solve single-start #(= % "ZZZ") input))
(defn part2 [input]
(solve multi-start #(str/ends-with? % "Z") input))