Skip to content

Commit bbd3bc2

Browse files
committed
day07 part 1
1 parent 8c91960 commit bbd3bc2

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

.idea/libraries/Leiningen__org_clojure_math_combinatorics_0_1_6.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clj-adventofcode-2019.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<orderEntry type="library" name="Leiningen: nrepl:0.6.0" level="project" />
1818
<orderEntry type="library" name="Leiningen: org.clojure/clojure:1.10.1" level="project" />
1919
<orderEntry type="library" name="Leiningen: org.clojure/core.specs.alpha:0.2.44" level="project" />
20+
<orderEntry type="library" name="Leiningen: org.clojure/math.combinatorics:0.1.6" level="project" />
2021
<orderEntry type="library" name="Leiningen: org.clojure/math.numeric-tower:0.0.4" level="project" />
2122
<orderEntry type="library" name="Leiningen: org.clojure/spec.alpha:0.2.176" level="project" />
2223
</component>

project.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
66
:dependencies [[org.clojure/clojure "1.10.1"]
77
[org.clojure/math.numeric-tower "0.0.4"]
8+
[org.clojure/math.combinatorics "0.1.6"]
89
]
910
:main ^:skip-aot clj-adventofcode-2019.core
1011
:target-path "target/%s"

resources/day07_input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3,8,1001,8,10,8,105,1,0,0,21,46,63,76,97,118,199,280,361,442,99999,3,9,102,4,9,9,101,2,9,9,1002,9,5,9,101,4,9,9,102,2,9,9,4,9,99,3,9,101,5,9,9,102,3,9,9,101,3,9,9,4,9,99,3,9,1001,9,2,9,102,3,9,9,4,9,99,3,9,1002,9,5,9,101,4,9,9,1002,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,5,9,101,3,9,9,1002,9,5,9,1001,9,5,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
(ns clj-adventofcode-2019.day07
2+
(:require
3+
[clj-adventofcode-2019.day05 :as day05]
4+
[clojure.math.numeric-tower :as math]
5+
[clojure.math.combinatorics :as combinatorics]
6+
[clojure.java.io :as io]))
7+
8+
;--- Day 7: Amplification Circuit ---
9+
;Based on the navigational maps, you're going to need to send more power to your ship's thrusters to reach Santa in time. To do this, you'll need to configure a series of amplifiers already installed on the ship.
10+
;
11+
;There are five amplifiers connected in series; each one receives an input signal and produces an output signal. They are connected such that the first amplifier's output leads to the second amplifier's input, the second amplifier's output leads to the third amplifier's input, and so on. The first amplifier's input value is 0, and the last amplifier's output leads to your ship's thrusters.
12+
;
13+
;O-------O O-------O O-------O O-------O O-------O
14+
;0 ->| Amp A |->| Amp B |->| Amp C |->| Amp D |->| Amp E |-> (to thrusters)
15+
;O-------O O-------O O-------O O-------O O-------O
16+
;The Elves have sent you some Amplifier Controller Software (your puzzle input), a program that should run on your existing Intcode computer. Each amplifier will need to run a copy of the program.
17+
;
18+
;When a copy of the program starts running on an amplifier, it will first use an input instruction to ask the amplifier for its current phase setting (an integer from 0 to 4). Each phase setting is used exactly once, but the Elves can't remember which amplifier needs which phase setting.
19+
;
20+
;The program will then call another input instruction to get the amplifier's input signal, compute the correct output signal, and supply it back to the amplifier with an output instruction. (If the amplifier has not yet received an input signal, it waits until one arrives.)
21+
;
22+
;Your job is to find the largest output signal that can be sent to the thrusters by trying every possible combination of phase settings on the amplifiers. Make sure that memory is not shared or reused between copies of the program.
23+
;
24+
;For example, suppose you want to try the phase setting sequence 3,1,2,4,0, which would mean setting amplifier A to phase setting 3, amplifier B to setting 1, C to 2, D to 4, and E to 0. Then, you could determine the output signal that gets sent from amplifier E to the thrusters with the following steps:
25+
;
26+
;Start the copy of the amplifier controller software that will run on amplifier A. At its first input instruction, provide it the amplifier's phase setting, 3. At its second input instruction, provide it the input signal, 0. After some calculations, it will use an output instruction to indicate the amplifier's output signal.
27+
;Start the software for amplifier B. Provide it the phase setting (1) and then whatever output signal was produced from amplifier A. It will then produce a new output signal destined for amplifier C.
28+
;Start the software for amplifier C, provide the phase setting (2) and the value from amplifier B, then collect its output signal.
29+
;Run amplifier D's software, provide the phase setting (4) and input value, and collect its output signal.
30+
;Run amplifier E's software, provide the phase setting (0) and input value, and collect its output signal.
31+
;The final output signal from amplifier E would be sent to the thrusters. However, this phase setting sequence may not have been the best one; another sequence might have sent a higher signal to the thrusters.
32+
;
33+
;Here are some example programs:
34+
;
35+
;Max thruster signal 43210 (from phase setting sequence 4,3,2,1,0):
36+
;
37+
;3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0
38+
39+
40+
;Max thruster signal 54321 (from phase setting sequence 0,1,2,3,4):
41+
;
42+
;3,23,3,24,1002,24,10,24,1002,23,-1,23,
43+
;101,5,23,23,1,24,23,23,4,23,99,0,0
44+
45+
46+
;Max thruster signal 65210 (from phase setting sequence 1,0,4,3,2):
47+
;
48+
;3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,
49+
;1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0
50+
;Try every combination of phase settings on the amplifiers. What is the highest signal that can be sent to the thrusters?
51+
52+
(def data-file (io/resource "day07_input.txt"))
53+
(def the-program
54+
(apply vector (map #(Integer/parseInt (clojure.string/trim-newline %))
55+
(clojure.string/split
56+
(slurp data-file) #","))))
57+
58+
(day05/execute-program-v2 the-program [3 0] [])
59+
60+
(defn execute-phase-seq-on-amplifiers [phase-seq program]
61+
(let [
62+
[_ _ amp-A-outputs ] (day05/execute-program-v2 program [(nth phase-seq 0) 0] [])
63+
[_ _ amp-B-outputs ] (day05/execute-program-v2 program [(nth phase-seq 1) (first amp-A-outputs)] [])
64+
[_ _ amp-C-outputs ] (day05/execute-program-v2 program [(nth phase-seq 2) (first amp-B-outputs)] [])
65+
[_ _ amp-D-outputs ] (day05/execute-program-v2 program [(nth phase-seq 3) (first amp-C-outputs)] [])
66+
[_ _ amp-E-outputs ] (day05/execute-program-v2 program [(nth phase-seq 4) (first amp-D-outputs)] [])
67+
]
68+
(first amp-E-outputs)
69+
))
70+
71+
(assert (= 43210
72+
(execute-phase-seq-on-amplifiers [4,3,2,1,0] [3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0])))
73+
74+
(assert (= 54321 (execute-phase-seq-on-amplifiers [0,1,2,3,4] [3,23,3,24,1002,24,10,24,1002,23,-1,23,
75+
101,5,23,23,1,24,23,23,4,23,99,0,0])))
76+
77+
(assert (= 65210 (execute-phase-seq-on-amplifiers [1,0,4,3,2] [3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,
78+
1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0])))
79+
80+
81+
82+
(defn find-max-thruster-signal [program]
83+
(first
84+
(sort-by
85+
(fn [[_ v]] (* -1 v))
86+
(map (fn [combination]
87+
[combination
88+
(execute-phase-seq-on-amplifiers combination program)])
89+
(combinatorics/permutations [0 1 2 3 4])))))
90+
91+
(def part1-solution (find-max-thruster-signal the-program))

0 commit comments

Comments
 (0)