16
16
17
17
; ; > For example, suppose you had the following scan output:
18
18
19
- ^{:: clerk/visibility {:result :hide }}
19
+ ^{:nextjournal. clerk/visibility {:result :hide }}
20
20
(def test-data " Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
21
21
Valve BB has flow rate=13; tunnels lead to valves CC, AA
22
22
Valve CC has flow rate=2; tunnels lead to valves DD, BB
@@ -36,10 +36,10 @@ Valve JJ has flow rate=21; tunnel leads to valve II")
36
36
37
37
; ; Start by parsing the test-data into a map where each valve is a key, and the value is a map listing the pressure and neighbouring nodes:
38
38
39
- ^{:: clerk/visibility {:result :hide }}
39
+ ^{:nextjournal. clerk/visibility {:result :hide }}
40
40
(require '[clojure.string :as str])
41
41
42
- ^{:: clerk/visibility {:result :hide }}
42
+ ^{:nextjournal. clerk/visibility {:result :hide }}
43
43
(defn parse [data]
44
44
(->> data
45
45
str/split-lines
@@ -55,15 +55,15 @@ Valve JJ has flow rate=21; tunnel leads to valve II")
55
55
:pressure pressure}])))
56
56
(into {})))
57
57
58
- ^{:: clerk/visibility {:code :hide }}
58
+ ^{:nextjournal. clerk/visibility {:code :hide }}
59
59
(clerk/example
60
60
(parse test-data))
61
61
62
62
; ; To find the shortest path we'll use the excellent Ubergraph library:
63
63
64
- ^{:: clerk/visibility {:result :hide }}
64
+ ^{:nextjournal. clerk/visibility {:result :hide }}
65
65
(require '[ubergraph.alg :as ua])
66
- ^{:: clerk/visibility {:result :hide }}
66
+ ^{:nextjournal. clerk/visibility {:result :hide }}
67
67
(defn shortest-path [start-pos end-pos data]
68
68
(ua/shortest-path (fn [coord]
69
69
(map (fn [neighbour] {:dest neighbour}) (-> data coord :next )))
@@ -73,7 +73,7 @@ Valve JJ has flow rate=21; tunnel leads to valve II")
73
73
; ; Once refactored and shrunk down, this function ended up being almost
74
74
; ; a copy-paste of [@volesen's solution](https://github.com/volesen/aoc2022/blob/main/day16/day16.clj#L48C10-L48C10)
75
75
76
- ^{:: clerk/visibility {:result :hide }}
76
+ ^{:nextjournal. clerk/visibility {:result :hide }}
77
77
(defn max-pressure [time-remaining start non-zero-valves elephant? data]
78
78
(let [next-nodes* (keep (fn [node]
79
79
(let [time-to-open (inc (:cost (shortest-path start node data)))]
@@ -89,31 +89,31 @@ Valve JJ has flow rate=21; tunnel leads to valve II")
89
89
elephant? (conj (max-pressure 26 :AA non-zero-valves false data)))]
90
90
(+ (* time-remaining (-> data start :pressure )) (apply max 0 next-nodes))))
91
91
92
- ^{:: clerk/visibility {:result :hide }}
92
+ ^{:nextjournal. clerk/visibility {:result :hide }}
93
93
(defn part-1 [data]
94
94
(with-redefs [shortest-path (memoize shortest-path)]
95
95
(let [data (parse data)
96
96
non-zero-valves (set (remove (fn [valve] (zero? (:pressure (valve data)))) (keys data)))]
97
97
(max-pressure 30 :AA non-zero-valves false data))))
98
98
99
- ^{:: clerk/visibility {:code :hide }}
99
+ ^{:nextjournal. clerk/visibility {:code :hide }}
100
100
(clerk/example (= 1651 (part-1 test-data)))
101
101
102
102
; ; Applying it to our input we get:
103
- ^{:: clerk/visibility {:code :hide }}
103
+ ^{:nextjournal. clerk/visibility {:code :hide }}
104
104
(clerk/code
105
105
'(= 1896 (part-1 (slurp " input/2022/16.txt" ))))
106
106
107
- ^{:: clerk/visibility {:result :hide }}
107
+ ^{:nextjournal. clerk/visibility {:result :hide }}
108
108
(defn part-2 [data-string]
109
109
(with-redefs [shortest-path (memoize shortest-path)]
110
110
(let [data (parse data-string)
111
111
non-zero-valves (set (remove (fn [valve] (zero? (:pressure (valve data)))) (keys data)))]
112
112
(max-pressure 26 :AA non-zero-valves true data))))
113
- ^{:: clerk/visibility {:code :hide }}
113
+ ^{:nextjournal. clerk/visibility {:code :hide }}
114
114
(clerk/example (= 1707 (part-2 test-data)))
115
115
116
116
; ; Applying it to our input we get:
117
- ^{:: clerk/visibility {:code :hide }}
117
+ ^{:nextjournal. clerk/visibility {:code :hide }}
118
118
(clerk/code
119
119
'(= 2576 (part-2 (slurp " input/2022/16.txt" ))))
0 commit comments