|
18 | 18 | (= pt end) (recur stack' (max longest (count seen)))
|
19 | 19 |
|
20 | 20 | :let [curr ((trails y) x)
|
21 |
| - seen' (conj seen pt) |
22 |
| - nbs (for [[nx ny] (aoc/neighbours 4 pt) |
23 |
| - :when (and (not (seen [nx ny])) |
24 |
| - (not (#{\#} ((trails ny) nx))))] |
25 |
| - [[nx ny] seen'])] |
26 |
| - (= curr \.) (recur (into stack' nbs) longest) |
| 21 | + seen' (conj seen pt)] |
| 22 | + (= curr \.) (recur (reduce (fn [s nb] (conj s [nb seen'])) |
| 23 | + stack' |
| 24 | + (aoc/neighbours 4 pt |
| 25 | + (fn [[nx ny]] |
| 26 | + (and (not (seen [nx ny])) |
| 27 | + (not (#{\#} ((trails ny) nx))))))) |
| 28 | + longest) |
27 | 29 |
|
28 | 30 | :let [pt' (if (= curr \>) [(inc x) y] [x (inc y)])]
|
29 | 31 | (seen pt') (recur stack' longest)
|
30 | 32 |
|
31 | 33 | (recur (conj stack' [pt' seen']) longest)))))
|
32 | 34 |
|
33 | 35 |
|
34 |
| - |
35 |
| -(defn int-hash [x y] |
36 |
| - (+ (* 150 x) y)) |
37 |
| - |
38 | 36 | (defn compress-graph [trails]
|
39 | 37 | (let [adjacencies (atom (i/int-map))
|
40 |
| - size (count trails)] |
| 38 | + size (count trails) |
| 39 | + int-hash (fn [x y] (+ (* 150 x) y))] |
41 | 40 | (doseq [[y row] (map-indexed vector trails)
|
42 | 41 | [x c] (map-indexed vector row)
|
43 | 42 | :when (not= \# c)
|
44 |
| - [nx ny] (aoc/neighbours 4 [x y]) |
45 |
| - :when (and (aoc/inside? size nx ny) |
46 |
| - (not= \# ((trails ny) nx)))] |
| 43 | + [nx ny] (aoc/neighbours 4 [x y] |
| 44 | + (fn [[nx ny]] |
| 45 | + (and (aoc/inside? size nx ny) |
| 46 | + (not= \# ((trails ny) nx)))))] |
47 | 47 | (swap! adjacencies update (int-hash x y) assoc (int-hash nx ny) 1))
|
48 | 48 | (doseq [pt (keys @adjacencies)
|
49 | 49 | :let [nbs (@adjacencies pt)]
|
|
74 | 74 | seen' (conj seen pt)]
|
75 | 75 | (= pt exit) (recur stack' (max longest score))
|
76 | 76 |
|
77 |
| - (recur (into stack' |
78 |
| - (for [[nb cost] (adjacencies pt) |
79 |
| - :when (not (seen nb)) |
80 |
| - :when (if (= 3 (count (adjacencies pt))) |
81 |
| - ;; if on a perimeter, go only right and down |
82 |
| - ;; makes everything 3x faster |
83 |
| - (or (> nb pt) |
84 |
| - (> (mod nb 150) (mod pt 150))) |
85 |
| - true)] |
86 |
| - [nb seen' (+ score cost)])) |
| 77 | + (recur (reduce (fn [s [nb cost]] |
| 78 | + (if (and (not (seen nb)) |
| 79 | + (if (= 3 (count (adjacencies pt))) |
| 80 | + ;; if on a perimeter, go only right and down |
| 81 | + ;; makes everything 3x faster |
| 82 | + (or (> nb pt) |
| 83 | + (> (mod nb 150) (mod pt 150))) |
| 84 | + true)) |
| 85 | + (conj s [nb seen' (+ score cost)]) |
| 86 | + s)) |
| 87 | + stack' |
| 88 | + (adjacencies pt)) |
87 | 89 | longest)))))
|
88 | 90 |
|
89 | 91 |
|
90 | 92 | (defn solve [input-file]
|
91 | 93 | (let [trails (aoc/parse-input input-file :chars)
|
92 |
| - adjacencies (compress-graph trails)] |
93 |
| - [(part-1 trails) |
94 |
| - (part-2 adjacencies)])) |
| 94 | + adjacencies (compress-graph trails) |
| 95 | + p1 (future (part-1 trails)) |
| 96 | + p2 (future (part-2 adjacencies))] |
| 97 | + [@p1 @p2])) |
95 | 98 |
|
96 | 99 |
|
97 | 100 | (solve (aoc/read-file 23))
|
0 commit comments