DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 7: Amplification Circuit

Collapse
 
a_wish_of_stars profile image
Akansha Yadav

Hi,
Someone please help me understand part 2 of day 7
I am trying to get the answer given in first example of part two.
The input is
3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,
27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5
For sequence 9,8,7,6,5
First input I gave was 9 as phase setting
Then gave 0 at the second occurrence of opcode 3
The program proceeds but it encounters opcode 5 at position 23 and resets my address pointer to 6. At 6 again the opcode is 3. Now here I give 8(next phase setting) or previous output value? Also in this way my program is not reaching 99 and it kept running until i killed it.

Collapse
 
jbristow profile image
Jon Bristow

Roughly I think:

  • The input of an amp is tied to the output of the previous amps output (A goes to B, C goes to D, E goes to A)
  • if an amp has no inputs, it needs to suspend until it has some.
  • there’s nothing preventing an amp from executing if it doesn’t need an input

So... when the first amp gets to its third or later input, it needs to wait until it receives an output from the last amp.

Once all amps reach 99, the answer is the last outputted value from the last amp.

Collapse
 
a_wish_of_stars profile image
Akansha Yadav

Got the output. Its 3:30 AM and I can finally sleep now.

Thread Thread
 
gambledrum profile image
John Persico

Hello...
Believe it or not, I'm still not getting how Part 2 works... I like the way you spelled things out in your post per the sample data. Enter 9, then enter 0, etc. Can you do something similar with the understanding you've gained here? I would truly appreciate it. Thanks.

Thread Thread
 
jbristow profile image
Jon Bristow

My attempt:

In part 1, a sequence of 2 amps (A0,A1) will operate like so:

A0-Start
A0-asks for phase number (0)
A0-asks for input (0)
A0-outputs oa1
A0-ends
A1-starts
A1-asks for a phase number (1)
A1 asks for input (oa1)
A1 outputs oa2 <—answer
A1 ends

Part 2 is... complicated to diagram, but I will try (again with two amps, A5 and A6)

A5 A6
Start Start
input read: phase (5) input read: phase (6)
input read from init: 0 input needed
output o1a5
input needed input read from A5: o1a5
output o1a6
input read from A6: o1a6 input needed
output o2a6
end input read from A5: o2a6
output o2a6 (answer)
end

Obviously the prompt uses 5 amps, and your input code produces a larger number of outputs per amp than my example. But I think this is the minimal state diagram possible that shows off the difference.

Please feel free to ask clarifying questions!

Thread Thread
 
gambledrum profile image
John Persico

Thanks for replying... I'm a new programmer, and I'm not sure why, but things still aren't making sense. Let me tell you what I'm doing. First of all, Part 1 works fine for me. This is what I do for Part 2 (sample 1).

Sample one is:

Max thruster signal 139629729 (from phase setting sequence 9,8,7,6,5):
3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,
27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5

So...
I start the program, I guess it would be on Amp A. It asks for input, and I give it 9 (from the phase setting sequence above). Then it asks for a second input, and I give it 0 (from the instructions in Part 2). At that point I get an output of 5 and the program stops... Here I have a few questions... 1) should the program have stopped? 2) When I start up the program on Amp B do I start with fresh data (above), or do I use the updated data (I'm using a hash table).

So, then, I guess I start Amp B; it asks for an input. I give it 8, the second number in the phase setting sequence. It asks for another input, and I give it a 5 (from the output above). At that point the program stops and outputs 14. So, at this point I have two amps that have already stopped. This goes on until I get to the fifth amp and I get an output there. But, Amp A has already stopped (it stopped right away). So, what do I do with the output I just got from the fifth amp? It's not the solution; it's a low number.

Hopefully, some of this makes sense. If I can't get the sample to work, I don't know how I'm going to get the puzzle data to work. Any replies would be very welcome.

Thread Thread
 
jbristow profile image
Jon Bristow

The key to debugging what you have here is probably knowing why your program stopped.

In my initial version of the IntCode interpreter would explode when it tried to read input when there was none to give it.

Each Amp should "pause", but not stop when it needs input that isn't there.