-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday07.clj
More file actions
42 lines (34 loc) · 1.06 KB
/
day07.clj
File metadata and controls
42 lines (34 loc) · 1.06 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
(ns day07
(:require
[aoc-utils.core :refer [empty-queue]]
[clojure.math.combinatorics :as combo]
[intcode :as ic]))
(defn initialize-amps [amps phase-setting-sequence]
(into empty-queue
(map (fn [amp phase]
(ic/send-to-in-queue amp [phase]))
amps
phase-setting-sequence)))
(defn run-amps [amps]
(loop [amps amps
output 0]
(if (every? #{:halted} (map :status amps))
output
(let [curr-amp (ic/in-run-out (peek amps) output)]
(recur (conj (pop amps) curr-amp)
(:output curr-amp))))))
(defn find-highest-signal [amps phase-settings]
(reduce
(fn [highest-signal phase-setting-sequence]
(-> amps
(initialize-amps phase-setting-sequence)
run-amps
(max highest-signal)))
0
(combo/permutations phase-settings)))
(defn solve [filename]
(let [computer (ic/initialize-from-file filename)
amps (repeat 5 computer)]
[(find-highest-signal amps (range 5))
(find-highest-signal amps (range 5 10))]))
(solve 7)