diff options
author | Leah Neukirchen <leah@vuxu.org> | 2021-12-22 19:17:02 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2021-12-22 19:17:02 +0100 |
commit | d424225868bdb90da017535d0344757ad3bc4021 (patch) | |
tree | 57896a1e5d95daee122f9b64a7b2473d230b586c /day21.clj | |
parent | 2a3c0e7c55f644f7af7b8a814357dc6942ed3aac (diff) | |
download | adventofcode2021-d424225868bdb90da017535d0344757ad3bc4021.tar.gz adventofcode2021-d424225868bdb90da017535d0344757ad3bc4021.tar.xz adventofcode2021-d424225868bdb90da017535d0344757ad3bc4021.zip |
day21
Diffstat (limited to 'day21.clj')
-rw-r--r-- | day21.clj | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/day21.clj b/day21.clj new file mode 100644 index 0000000..d286dfe --- /dev/null +++ b/day21.clj @@ -0,0 +1,56 @@ +(ns org.vuxu.aoc2021.day21 + (:require [clojure.string :as str])) + +(def data + (->> (slurp "day21") + str/split-lines + (map (partial re-find #"\d+$")) + (mapv parse-long))) + +(def freqs + (frequencies + (for [a [1 2 3] + b [1 2 3] + c [1 2 3]] + (+ a b c)))) + +(defn move [roll player state] + (let [state + (update-in state [:pos player] #(inc (mod (dec (+ % roll)) 10)))] + (update-in state [:score player] + (get-in state [:pos player])))) + +(def state {:pos data + :score [0 0]}) + +(def part1 + (let [game (take-while (fn [state] + (every? #(< % 1000) (:score state))) + (reductions + (fn [state [player val]] + (move val player state)) + state + (map vector + (cycle [0 1]) + (map (partial apply +) + (partition 3 (cycle (range 1 101)))))))] + (* (* 3 (count game)) + (apply min (:score (last game)))))) +;; => 713328 + +(def play + (memoize + (fn [player state] + (cond + (>= (get-in state [:score 0]) 21) [1 0] + (>= (get-in state [:score 1]) 21) [0 1] + :else + (reduce (fn [result [val freq]] + (mapv + result + (mapv (partial * freq) + (play (- 1 player) (move val player state))))) + [0 0] + freqs))))) + +(def part2 + (apply max (play 0 state))) +;; => 92399285032143 |