-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday23.clj
More file actions
58 lines (44 loc) · 1.45 KB
/
day23.clj
File metadata and controls
58 lines (44 loc) · 1.45 KB
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
(ns day23
(:require [intcode :as ic]))
(defn- create-network [computer]
(into {}
(for [i (range 50)]
[i (-> computer
(ic/send-to-in-queue i)
ic/run-until-halt)])))
(defn- send-packets [network i]
(loop [[dest x y & queue'] (:out-queue (network i))
network network]
(cond
(nil? dest) (assoc-in network [i :out-queue] [])
(= 255 dest) (recur queue' (assoc network 255 [x y]))
:else (recur queue' (update network dest ic/send-to-in-queue [x y])))))
(defn- run-comp [network i]
(cond-> network
(empty? (:in-queue (network i))) (update i ic/send-to-in-queue -1)
true (update i ic/run-until-halt)
true (send-packets i)))
(defn- run-network [network]
(reduce (fn [acc i]
(run-comp acc i))
network
(range 50)))
(defn- part-1 [network]
(loop [network network]
(if-let [[_ y] (network 255)]
y
(recur (run-network network)))))
(defn- part-2 [network]
(loop [network network
prev-y nil]
(let [network' (run-network network)]
(if (every? (comp empty? :in-queue second) network')
(let [[x y] (network' 255)]
(if (= y prev-y)
y
(recur (update network' 0 ic/send-to-in-queue [x y]) y)))
(recur network' prev-y)))))
(defn solve [filename]
(let [network (create-network (ic/initialize-from-file filename))]
[(part-1 network) (part-2 network)]))
(solve 23)