-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.clj
114 lines (105 loc) · 13.7 KB
/
day17.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(ns aoc.day17)
(def input ">>><<<<>><<<<>>>><<><<<<>><><<<><<<<>>><<<<><<>>>><<<<>><<<<>><<<>><>><<<>><>><<>>><<>><<<>>><<<<>>><<<<>>><<<<><<<<><>>><<<>>>><<>><<<><<<<><<<>>><<<<>>>><<<><<<><>>><<><>><<<<>>><>>>><<>>>><<<<><<<<><<<<>>><<>><><<<>><<<><<>>><<>>><>><>>><>>>><<<<>><<>>><<<>><<><<>><<<<>>><>><><>>>><<<<><>><<>><>>>><<>>><<>>><<>>><><<<<>>><<<>>><><<>>>><<<>><<<>>>><<>><<<>><<<>>>><>>>><<>><<<<>><<<>>><<>>><<>><<<>>>><<>><<<>><>>>><>><<<>>>><><<>>><<>>>><>><><<<<>>><<>><<>><<<<>><>>>><<<<>>>><<<>>><<<<>><<<<>>><<<<>>><<>>>><<>><>>><<><<<>>>><<<<>><<>><><<<<>>>><<<<><<<>><><<<><<>><>><<<<>><<><<<>><>><>><>>>><<<><>>><>>>><<<<>>><<<>>>><>>><>>><<<><<<>>><<<<>>><<><<<<>>>><<<<><<>>>><<>><<<<>>>><>>>><<>><<>><<><>>>><><><<<<>>><<<>>><<<<>>>><<>>><<<<>>>><><<><<<<>>><<<<>>>><<<>><>><<<<>>>><><>>>><>>><<><<<<>><<<<>>>><<>>><<>>>><>>><<>>><><<<<>>><<<>><>>>><<<><<<<>>>><<<<>><>><<>>>><<><>><>>><<>>>><<<<>>><<<<>>><<<>><<>><<>>><>><>><<<<>><<<>><><<<>><<<>>>><<<<><<<<>>><><<<><<>>><<<>>><<<<>>><<>>><<<><<>>><<<>>>><<<<>>><>>>><<>><>><<<<>><<<<>>>><<>><<<<><>><<<<>>>><>>><<<<>>><>><<<<><<<><<<>><><<<>>>><><<<>><>>><<<<>>>><<<<>>>><<>><<<<>><<<><<>>>><<<>>><<<><<><<<<>>>><<><<>><<>>>><<<<><><<<<>>>><<<<>><>>><<<>>><<<>><<<>><<>><<<<>>><<<<>>>><<<><>>>><<>><<>>>><<<><>>><><><<<>><<><<<><<<<>>>><<<>>><<<>>><<<<><><<<>>><<<<>>>><<>>>><<>><<>>><>>><<<>><<<<>>><<<<>><<<<>>><<><<<>>><<<>><<><<><<<<>>>><>>>><<>>><<<<>>>><<<<>><<<>><<<<>>>><<<>>>><<><<<><<<>>>><<<<>>><<<<>><<>>>><>><><<<>>><<<>>>><<<<>><<>>><><>><<<>>><<<>>>><<>>>><<<<><><>>>><<>><<<>><<<<>>><<<>>><<<>>><<<>>>><<>>><<>>>><><><>><<<<>>><<<<>>>><<<><><<<<><<>><<<<>><<><<<><<<>><<><<<>>>><>><><>><<<>>><<<<>>>><<<<>><<>><><<<>>><>>>><>>><<<<>>><><<<><>>>><<<><<>><<<>><>>><><<<<>><<>>>><<>>><<<>>><<<<>><>>><>>><<<>>>><<>>><><<<><<<>>><<<>>><<<<>>>><<<>><>>>><<<<>>>><<<>>>><<>>>><<<><<<<>><<<<><<><<>>>><<<<>><><>>><<>>>><>>><<<<>>><<<<>><<<><<<>>><<><<>>><<>><<<>>>><>>><<<<>>><<<>>><>>>><<<<>>><<<<>>><><<<<>>><><<<><<<>>>><<><<>><><<<<>>>><<<<>><<>>>><<><<<<>>>><<<<>>>><>>><<<>>>><<<<><>><<><<<>>><<<<><<<<>>>><>><>>><<<>>>><><<<<><<<<>><<<>><<<<><<<>>><><<<<>><<>><<<>><<<>>><<><<<>>><<><><<<<>>>><<>><<<><>>>><>>>><<>>><>><><<<>>><>>>><><<<<><>>><<>>>><<>><<>>>><<<>>>><<<<>>>><<<>><<<<>><<><<<>>><>>><<<>>><<<<>><<>>>><>>><<>>><<<>>><>><>>><<>><<<<>>><>><<>>>><<<<><<<<>>><<<><><><>>><<>>><<<<>>>><<<<>><>><<<<>>><<>><>>><<<>>>><<<<>>>><><><<<>>>><><<<>>><<<<>>>><<>>>><<<>>>><<><<<>>><<<<>>><<>>>><><>>><><>>><<<<>>>><<<>><<>>>><<><<>>><>><><>>><<<<>>><<>>><<<<>>><<<>>><>><<>>><<<>><>>>><<<>>><>>><<<>>>><<<<>>>><<<<><<<>>>><<<>>><<>>>><<<<><<<<>>><<<>><<<><<<>>><<<<>><<>><<<>>>><>>>><<<<><<<<>><<>><<>><<<><<<<>><<<>><<<<>>>><>>>><>><><<>>>><<<<>><<>>>><>>><<>>><<<<>>><<<>>><<<>>>><<>>>><>>>><><>><<<>><<<<><<<<>>>><<<>>>><>>><<<><<>>>><<<>>><>>>><<><<<><<<<>><<>>>><<<>><<<<><<><>>><>><>><>>>><<<>>><<>><<<<>>><<<>><<<<>>>><><<><<<>>>><<>>><<<><<><<<><<<<><>>><>><<<><<<>><<<><<>><<<<>>><<>>><<>>><<<<><>><<><<<<>><<<<>><<<<><>>><<<<>><<>>>><>><>>>><<<<>>><<<<>><><<<>><<>>>><>>>><<>>><><<>>>><<><<>><<<>><>>>><<<<>><<<><<><<>><<<>><<<>><>><>>>><<<<>>><<<<>>><<<>>>><>><<<>>>><<<<><>>><>>>><<>><<<<>><<<<>>><<<<>>><<<<><<<>>>><>>>><<<>>><<<>>>><<<>>>><<<<>>><<<<>>><<<><>>><<<<>>>><<<<><>>>><<>><<<>>>><<><<<<><>><><<>>><<<<>>><<<<>>>><<<<>>>><<>><<<<>>><<<<>>>><<>>>><<<>>><<<<>>>><<<><<<<><<>><<<<>>><<<<>>><>>>><>><<<<><<<>>><>>>><><<<><<<>><>>><<>><>><<<><>>><<><<<>>><>>><<<<><>>><<><<><<<<><<<>>><<<<>>><>>>><>><<<<>><<>><<<<>>><<<>><>><<<><>>>><>><<><><>>>><<<<><>>><<><<<<>><<<<><>>>><<>>><<<>><<<>>>><<>>><>>>><<<>>>><<>>><<<>>>><<<>><>>>><<><>>>><>>><>>>><<<>>>><<<<>>>><<>>><<>>><<<<>>>><>>>><<><>><<<<>>><><<<><<<<>>>><<<<>>>><<>>><<<>><<<>><<<>>>><<<<>>>><<<<>><<>>><<><<>>>><>><>><>>><<>><<<>>><<<<>><<>><<<<>>><>>>><<<<><<<>>>><>>><<>><>>>><>>>><><<<<>><<<><<<<>>>><<<>>><<<<>>>><<<<>>><<<>>><>>><<<>>>><>>>><<<<>>><<<<><<><<<<><<<<><>>>><<><>>>><<>><<<>><>>><<<<>><>><<<><<>>>><<>>>><>>><<<><<<>><>>><><<>>><<><<>>><<<>>>><<<<>><<><<<<>>>><<<<>>>><<<<><<<<>>><<<<><<<><>>><<>>>><<>>>><<<<>>><<<>>><<<>>>><<<<>>><>>><<>>>><<<<>>>><<<>><<<>>>><<<>><><<<>><><<<>>><><<<><>>>><<>><<<<>>>><<<>>>><<>>><<<<><<<<>>>><<>>><<<<>>>><>>><<<>>><<<<>>>><<>>><<>>><<>>><<<>><<><<>><><>><>>>><<<<>>><<<<>>>><<<><<>>><<<><<<>>>><<<<>>><<<<>>><<<<>><<<<>>><<<<>>><<<><<<><<<<><<>>><><<<>>><<>>>><<>>><>>>><<<>>>><<<<>>><>>>><<>><<<>>><<<>><>><<><>>><<<><<<<>>>><<>><<<>><>>>><<<>><<>><<>>>><><>><>>>><<<><<<<><><>>>><<>>>><<<>>><><<>><<<<><<<<>>><>>><<>><<<>>>><<<<>>>><><<<<>><<<<>><<><<>>>><<><<<<><>>>><<>><<<>><>><<<<><>>>><>><<>><<>>>><<<<>>><<<<>>>><<>>>><<<<>>><<<>>><><<<>>>><><>>><<<><<><<<<><<<>>><<>>><<>>>><<<><<<<>>>><<<>>>><<><<<<>>>><<<<>>><<<>>><<>>><><>>><<<<>>>><<><<<>>>><<<<><<><><<><<<<>><<<><<<<>><<>><<>>><>><>><<<<>>>><>>><><<>>>><<<>>><<<<>>>><<<<><>>>><<<>><>>><<<<>>>><<><<>>><<><>>><<>><<<<>><<<>><<>><<<><<>>>><<>><>>><>>><<<>><<<<>><<<><<>><<<<>>>><<<<>>><><<<<>>>><<><>>><<<>>>><<><>>>><<<<>><>><<<>><<>><<<<>>>><<<>>>><<<>><>>>><>>>><>>>><<<<>>>><<>>>><<<><<>>>><<><<<><<<>>>><<<<>>>><<<<>><>>>><>>><<><<<><>><<>>><>>>><<>>>><<<<>>>><>>><>><<>><<<>><<<><><<<<>>><<<<><<>>>><>>><<>><<>><<<<><<<<><<<>><<<<>>>><<><<<><<<<>>><<<>>>><<<<>><>><<<><><<<<>><<<<>><>>>><<<<>>><>>>><>><<>>>><<<>>><<>>><>>>><>><>>><>>><<><<>>><<>><<<>><<<>><<<>>><><>>><>>>><<>>>><<<<>><>>>><>>>><>><<>>><<<<>>>><<>><><<>>><<<>>><<<>>>><<><<<<><>>>><>>><<<>>><<<><<<<>>><>>>><<>>><<>>>><><<>><<>>><>>><>>><<><<<><>><<<>>><>><<<><<>>>><<<>>><<<<>><<>>>><>>>><<<<>>><<<<><<<<>><<<><<>>>><><<<<>><<<><<<>>>><<<>><<<<>>><<<><<<>>>><<><>>><<<>><<<<><<<>>><<<>><>><<<>>>><>>><<><<>>><<<>>>><<<<>>><<<<>>><<<<><><><<>>><<>><<<>>>><<<>>>><><<<>>>><>>><<>><<>><>>><><<>><<<<>><<<<>>><<><<>>><<>>><<><<<<><<<>><><<>>>><<<<>>><<<>>><<<<><<<<><<>><<<><<>>>><>>><<<>>>><<<>>>><>>><>><<>><<>>><<>><<<<>><<>><<<>>><<<<><>>><>><<<><<>><<<<><<<><<>><<>>>><>>>><<><<><<>><>>>><><><<<<>>><><<>>>><<<><<<<>><<<<>><><<<><>>>><<<>>>><<>><<<>>><<>>>><<<<>>>><><<<>>><<<<><<<><<<><<<<>>>><>><<<<>>>><>>>><<<<>>><>>>><<<<>><<<>>>><<<<>>>><<>>>><<>><<<<>><<<><<<<>>><>><<<<>><>>><<<>>><<<<>>>><>><<><<>><><<<<>>>><<<<>><>>>><<<>>>><<>>>><>>>><<><>>>><<<<>>>><<<<>><<<<>>><><<><<<><<<<>>><>><<>>><<>>>><<<>>>><<>>><<<>>>><<><>>>><<<><><<<><<<>><<<><<<<>>><<><<<<>><>>><>>><>>><><>><<>>><<<<>>><>>><<<<><<<>>>><<>><<<<>>>><<<>><><<<<><<<<>>>><<>><<>>>><<>>>><><<<<>>><>>><<>><<<<>>>><>>>><<<<>><<>>><<<>>><>>>><<<>>>><<<><<><<><<<>>>><>>><<<>><<<>><<<<>>>><<<>><<<<>>><<>>><<<>>>><><<>>>><<<>>><<<>>>><<<<>><<>>><<<<>><<>>><>>><<<<><>>>><<<><>>>><<<<>><<<<><>>>><<<<>><>><<<<><<>>><<<<>>><><<<<><>>>><<<<><<<<>><<<<>>><<>>><<<<><>>>><<>>>><><<<>>>><>><<<>>><<<<><<>>>><<<<>>><><>>><>><<<>><<<<>>>><>>>><<<<>>><<<<>>><<>><<<>>>><<<>>><<>><>><><><<<><><<<>>><<<<>><<<<>><>>>><<>><><<>><<<<>>>><>><>>><>>><<<>>><<><>><><<<><<>>>><<<<>>>><>>><<<<>><<<>>><><<<<><<<>><<><<<><<>>><<<>>><<<>>>><><<>>><<<<><<<>>>><<>>><>><<<<>>>><<<<>>>><<>>><<>>>><<<<>><<<<>>>><<<<>><<>><>>><<>>><<>>>><<>>>><<<>>>><<<<>>>><<<><<<>>>><<<<>>>><<>>>><<<<>>>><<<><><>>><<>><<<<>>><<<<>>><><<><<<<><<><<<<>>><>>><<<<>>><<<<>><<<<>>>><<><<>><<>><<<<>>>><<<<>><><<>><>>>><>><><>>>><<<><<>>><<>>>><<><<<<><<>><>>>><>>>><<<<>>>><<<>>><<>><<<><<<>>><<>><<<><>>>><>>>><<>><>>>><<>><<<<>><>>><<>><<>>><<<<>>><<<><<<<>>><<<<>>><<<>><<>>><<<<>>><><<<<>>><<<<>>>><<<<>>>><><<>>>><<>>>><<<<>>>><>>><<<<><<>><<<>>><<<<>>>><>>><<<><<>>>><<<<>><<<><>>>><<<<>>><<<>>>><>>><<><>>>><<<<><<<<>>><<<>><<<><<<>>>><>>>><<<<><<>><<<><<>>>><<<>>>><>><<>>><<<><<<>><>>>><<>><<>>>><<<<><<<>><<<<><<>>><>><<<>>><<>><<><<<>>>><<<<>>><><<<<>><<>>>><<<<>>><>><<>><<>>>><<<><<>>>><<>>><<<>>><<<<><<<>><<<<>>><<>>><<<>><<<><<<<>>>><<<><<>><<<>><<>>><>>>><<<>>>><<>><<>>>><<>><<<<><>>>><<<>>>><<>>>><<<>><<<<><<<<>><<<<>>><<<<><<<<><<<><<<<>><<<<>>>><<<<>>><<<<>>><>><><><<>>><<>><<<<>><>>>><>><<<>>><>><>>><<<><<<><>>>><<><>><<>><><<>><<><<<>><<<>>><<>><<<><><<>><<<>><<<><<<>><>>><<<>>><<<>>><>>>><>>>><<<><>><<>><<<<><<<<>><<<<>><<<>>><<<>><<><<><<>><<><<><>>>><<<><<><<>>><<<>><<<>><<>>><<<<><<<<>>><<<<>><<><>><<>><<><<<><<<>>><<>>>><>><<<<><<<<>>>><<<<>>><>>><<>>>><<>>>><<>>><<<>><<<><<<<><<<<><<<<>>><<<>>>><<<<>><>><<<>>><<>>><<<<><>>>><<>>><<<<>>><<>><><<>>>><><<<>><>>><<><<<>>>><<<>>><<<><<<>>><>><<<><<<<>><<<<><<><<>>>><><<<<>>>><>><<>><<><<<><<<<>>><><<><<<<>>>><><<<<>>>><<>><<<>>>><>>>><<<<>>><<>>><><<<<>>>><><>>><<>><<<<>>><<<<><<<<><><<<<>>><<<<>>>><<><<>>>><<<>>><<>>><<><<<<><<>><>>><<<>>>><<<><<<<>>><<<>>>><<<<>><<<<>><<<<>><<<<><<<<>>>><<<<>>>><<<>>><<<>>><<<<>><><<<<>><>>>><<<<><>>>><<<<>><<>>><>><<<><<<<>>>><><<<><<>>>><<<<><<<><>>><<><>>>><<<<><<>>><<>>><<>>><<<>>><>>>><<<>>>><><<<<>>><<<<>>><<<<>>>><<<>>>><<>>><<>>>><<<<><>>>><>><<<<>>><<<<>>>><<<>>><<<>><<<>><<<>><>><>>>><>><<>><<<<><>>><<<<>>><>>><>>>><<<<>>><>>>><<><<<>>><>>><<<<>><<<>><<<<>><<<>>>><>>>><<<>>><<<<>>>><<<<>>>><<<>>>><><<<><<<>>>><<<><<<<>>>><<<>>>><>>><<<<><<<>><<<><<>><<>><<<<><<>>>><<<>>>><<<<>><>><>>>><>><<<><<><<<>>><<<<>>><<>><<<<>><><>><>><<>><>>>><<>>>><<>>><<<><>>><<<>>>><>><>>>><<<<><<>>><<<>>><<>>><>><><<<<>><><>>>><>><<<>><<<><<><>>><<<<><<<<>>><<>>>><<><<<<>>><<<>><<<<>>><<>>>><<>>>><<>><<<<>><<><<<><<<<>>>><><<>>><<<>>>><>>>><<><<<<>>>><<<>><>>><<<<>><<<<>>><<<><>>><<<>>><>>><<<<>><>>>><<<<>>><<><<<<>><<><<<<><<>><<<>>>><<<>><<<>>><<<>>>><<<<><<<>><<>>>><<>>>><>><<<<>>>><>>><<<><<>>><<<><<>>>><<<>>><<<>>><>><<<>>><<<<>><<>>>><<>>><<>>><<<>>><><<<>>><<>>>><>>><<>>><<>><<<><>>>><<>>>><<<<>><<>>><<<<>><<>><<>><<<>><<><>>><><<<>>>><><<>>><<>>><<<>>><<><><<<<>>><<<<><<>>>><<>><<<>>><<<>>>><>>><<<>>><<<<><<<>>><<<>><>><<><<<<>>><<>>>><<>><><<<<>>><<<<>>><<<<>>><<>><<>><<>><>><<<><>>><><>>>><<<>>><>>><<>>><<<><<>>>><<<><<<<>>><<<><<<><<<<>><<<<>><>>>><<<>>><<<><<<<>><<<<>><><<<<><<<<><<<>><<<>>>><>><<<>>><<>>><>><<<<>><<<><><<<<>>>><<<<>><<<>>>><<<<><<<>>>><<<<>>><<<>>><<>>>><>>>><<<<>><<<<>><<><<>><>>>><<>>>><<>><>>><<>>>><><<><>>><<><<<<>>>><<>>><<<<>><>>><<<<>><<<>><>>>><<<><<><<<>>><>>>><>>><<<>>><<>>><<<<><<<>>><>>><><<<>><<<>>><<<<>><<>>><<<<><>><>><><>>><<>><<")
(defn parse-input [input]
{:jets (vec (map {\> 1, \< -1} input))
:jet-idx 0
:shape-idx 0
:max-y 0
:rocks #{}})
(def shapes
[[[0 0] [1 0] [2 0] [3 0]]
[[1 2] [0 1] [1 1] [2 1] [1 0]]
[[2 2] [2 1] [0 0] [1 0] [2 0]]
[[0 0] [0 1] [0 2] [0 3]]
[[0 1] [1 1] [0 0] [1 0]]])
(defn free? [world [x y]]
(and (>= x 0)
(< x 7)
(> y 0)
(not (contains? (:rocks world) [x y]))))
(defn visualize
([world]
(visualize world #{}))
([world coords]
(let [max-y (max (->> (map second coords)
(#(conj % 0))
(reduce max))
(->> (map second (:rocks world))
(#(conj % 0))
(reduce max)))
coords-set (set coords)]
(doseq [y' (range 0 (inc max-y))
x (range -1 8)
:let [y (- max-y y')]]
(print
(cond
(and (= y 0) (or (= x -1) (= x 7))) "+"
(= y 0) "-"
(or (= x -1) (= x 7)) "|"
(contains? coords-set [x y]) "@"
(free? world [x y]) "."
:else "#"))
(when (= x 7)
(println))))
(println)))
(defn place [world]
(let [shape-idx (:shape-idx world)
shape (get shapes shape-idx)
dx 2
dy (+ (:max-y world) 4)
original-max-y (:max-y world)]
(loop [coords (map (fn [[sx sy]]
[(+ sx dx) (+ sy dy)])
shape)
{jets :jets
jet-idx :jet-idx
rocks :rocks
:as world} (assoc world :shape-idx (mod (inc shape-idx) (count shapes)))]
(let [push-coords' (map (fn [[x y]]
[(+ x (nth jets jet-idx)) y])
coords)
push-coords (if (every? #(free? world %) push-coords')
push-coords'
coords)
fall-coords (map (fn [[x y]]
[x (dec y)])
push-coords)]
(if (every? #(free? world %) fall-coords)
(recur fall-coords
(-> world
(assoc :jet-idx (mod (inc jet-idx) (count jets)))
(assoc :max-y (max original-max-y
(->> (map second fall-coords)
(reduce max))))))
(-> world
(assoc :jet-idx (mod (inc jet-idx) (count jets)))
(assoc :rocks (into rocks push-coords))))))))
(defn part1 []
(let [world (parse-input input)]
(:max-y (nth (iterate place world) 2022))))
(defn world-hash [world]
(let [max-y (:max-y world)]
(hash (for [x (range 0 8)
y (range (- max-y 100) max-y)]
(free? world [x y])))))
(defn part2 []
(let [year 1000000000000]
(loop [world (parse-input input), states {}, n 0]
(let [world' (place world)
state-key [(:jet-idx world') (:shape-idx world') (world-hash world')]]
(if (contains? states state-key)
(let [first-occurrence (get states state-key)
cycle-start (:n first-occurrence)
cycle-length (- n cycle-start)
cycle-height (- (:max-y world) (:max-y first-occurrence))
missing-places (mod (- year cycle-start) cycle-length)
missing-height (- (:max-y (nth (iterate place world) missing-places))
(:max-y world))]
(+ (:max-y first-occurrence)
(* cycle-height (long (/ (- year cycle-start) cycle-length)))
missing-height))
(recur world'
(assoc states state-key {:n n
:key state-key
:max-y (:max-y world)})
(inc n)))))))