Skip to content

Commit d94efa1

Browse files
committed
Finished 07a in racket.
1 parent 290bb00 commit d94efa1

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

zenspider/2019/07.rkt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#lang racket
2+
3+
(require "../myutils.rkt")
4+
(require "int_code.rkt")
5+
6+
(module+ test
7+
(require rackunit))
8+
9+
(define (run-amp mem phases [input '(0)])
10+
(for/fold ([input input]
11+
#:result (first input))
12+
([phase (in-list phases)])
13+
(execute mem (cons phase input))))
14+
15+
(define (problem-07a input)
16+
(define mem (mem-parse input))
17+
18+
(for/fold ([max (void)])
19+
([phases (in-permutations (range 0 5))])
20+
(let ([result (cons (run-amp mem phases) (list phases))])
21+
(cond [(void? max) result]
22+
[(< (car max) (car result)) result]
23+
[else max]))))
24+
25+
(module+ test
26+
(define-simple-check (check-int-code/5 input program output)
27+
(check-equal? (execute program input)
28+
output))
29+
30+
(define input/1 "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0")
31+
(define input/2 "3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0")
32+
(define input/3 "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0")
33+
34+
(check-equal? (problem-07a input/1)
35+
'(43210 (4 3 2 1 0)))
36+
37+
(check-equal? (problem-07a input/2)
38+
'(54321 (0 1 2 3 4)))
39+
40+
(check-equal? (problem-07a input/3)
41+
'(65210 (1 0 4 3 2))))
42+
43+
(define (problem-07b input)
44+
#;
45+
(argmax car
46+
(for/list ([phases (sequence->list (in-permutations '(range 5 10)))])
47+
(cons (run-amp mem phases) (list phases))))
48+
#f)
49+
50+
(module+ test
51+
(check-equal? (problem-07b "123") 6))
52+
53+
(module+ test
54+
(displayln 'done))
55+
56+
(module+ main
57+
(define input (parse-file (data-file 07)))
58+
(problem-07a input)
59+
(problem-07b input))

zenspider/2019/desc/07.txt

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
## --- Day 7: Amplification Circuit ---
2+
3+
Based on the navigational maps, you're going to need to send more power to
4+
your ship's thrusters to reach Santa in time. To do this, you'll need to
5+
configure a series of amplifiers already installed on the ship.
6+
7+
There are five _amplifiers connected in series_ ((As you can see, I know
8+
exactly how rockets work.)); each one receives an input signal and produces
9+
an output signal. They are connected such that the first amplifier's
10+
output leads to the second amplifier's input, the second amplifier's output
11+
leads to the third amplifier's input, and so on. The first amplifier's
12+
input value is `0`, and the last amplifier's output leads to your ship's
13+
thrusters.
14+
15+
` O-------O O-------O O-------O O-------O O-------O
16+
0 ->| Amp A |->| Amp B |->| Amp C |->| Amp D |->| Amp E |-> (to thrusters)
17+
O-------O O-------O O-------O O-------O O-------O
18+
`
19+
20+
The Elves have sent you some _Amplifier Controller Software_ (your puzzle
21+
input), a program that should run on your existing Intcode computer. Each
22+
amplifier will need to run a copy of the program.
23+
24+
When a copy of the program starts running on an amplifier, it will first
25+
use an input instruction to ask the amplifier for its current _phase
26+
setting_ (an integer from `0` to `4`). Each phase setting is used _exactly
27+
once_, but the Elves can't remember which amplifier needs which phase
28+
setting.
29+
30+
The program will then call another input instruction to get the amplifier's
31+
input signal, compute the correct output signal, and supply it back to the
32+
amplifier with an output instruction. (If the amplifier has not yet
33+
received an input signal, it waits until one arrives.)
34+
35+
Your job is to _find the largest output signal that can be sent to the
36+
thrusters_ by trying every possible combination of phase settings on the
37+
amplifiers. Make sure that memory is not shared or reused between copies of
38+
the program.
39+
40+
For example, suppose you want to try the phase setting sequence
41+
`3,1,2,4,0`, which would mean setting amplifier `A` to phase setting `3`,
42+
amplifier `B` to setting `1`, `C` to `2`, `D` to `4`, and `E` to `0`. Then,
43+
you could determine the output signal that gets sent from amplifier `E` to
44+
the thrusters with the following steps:
45+
46+
* Start the copy of the amplifier controller software that will run on
47+
amplifier `A`. At its first input instruction, provide it the amplifier's
48+
phase setting, `3`. At its second input instruction, provide it the input
49+
signal, `0`. After some calculations, it will use an output instruction to
50+
indicate the amplifier's output signal.
51+
* Start the software for amplifier `B`. Provide it the phase setting (`1`)
52+
and then whatever output signal was produced from amplifier `A`. It will
53+
then produce a new output signal destined for amplifier `C`.
54+
* Start the software for amplifier `C`, provide the phase setting (`2`) and
55+
the value from amplifier `B`, then collect its output signal.
56+
* Run amplifier `D`'s software, provide the phase setting (`4`) and input
57+
value, and collect its output signal.
58+
* Run amplifier `E`'s software, provide the phase setting (`0`) and input
59+
value, and collect its output signal.
60+
61+
The final output signal from amplifier `E` would be sent to the thrusters.
62+
However, this phase setting sequence may not have been the best one;
63+
another sequence might have sent a higher signal to the thrusters.
64+
65+
Here are some example programs:
66+
67+
* Max thruster signal _`43210`_ (from phase setting sequence
68+
`4,3,2,1,0`):
69+
70+
`3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0`
71+
* Max thruster signal _`54321`_ (from phase setting sequence
72+
`0,1,2,3,4`):
73+
74+
`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+
* Max thruster signal _`65210`_ (from phase setting sequence
77+
`1,0,4,3,2`):
78+
79+
`3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,
80+
1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0`
81+
82+
Try every combination of phase settings on the amplifiers. _What is the
83+
highest signal that can be sent to the thrusters?_

zenspider/2019/input/07.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,38,59,84,97,110,191,272,353,434,99999,3,9,1002,9,2,9,101,4,9,9,1002,9,2,9,4,9,99,3,9,102,5,9,9,1001,9,3,9,1002,9,5,9,101,5,9,9,4,9,99,3,9,102,5,9,9,101,5,9,9,1002,9,3,9,101,2,9,9,1002,9,4,9,4,9,99,3,9,101,3,9,9,1002,9,3,9,4,9,99,3,9,102,5,9,9,1001,9,3,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,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,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,2,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,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,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,99,3,9,101,2,9,9,4,9,3,9,101,1,9,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,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1001,9,1,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,1002,9,2,9,4,9,3,9,1001,9,1,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

0 commit comments

Comments
 (0)