Did you get your IntCode machine just right on Day 5? Well, too bad, it was wrong.
Day 7 - The Problem
Looks like we need more power. And we're given some more parts to fix up our module to scrape a few more Joules out here and there. Unfortunately, our supply elves forgot to send us the documentation, and we have to find the optimal solution ourselves.
Part 1 is about finding the permutation of amplifiers that gives us the best output. Oh, and your IntCode compiler needs to be able to take multiple inputs now.
Part 2 decides that we need to create a feedback loop. I'm interested to see what people come up with for this one. Maybe we can fake the async?
This one swerves right back into hard territory for me. At the time of posting this, I've only solved part 1, and I may not be able to return to part 2 until Sunday! Calamity! (Ah well, such is life!)
Ongoing Meta
Dev.to List of Leaderboards
-
120635-5c140b9a
- provided by Linda Thompson
If you were part of Ryan Palo's leaderboard last year, you're still a member of that!
If you want me to add your leaderboard code to this page, reply to one of these posts and/or send me a DM containing your code and any theming or notes you’d like me to add. (You can find your private leaderboard code on your "Private Leaderboard" page.)
I'll edit in any leaderboards that people want to post, along with any description for the kinds of people you want to have on it. (My leaderboard is being used as my office's leaderboard.) And if I get something wrong, please call me out or message me and I’ll fix it ASAP.
There's no limit to the number of leaderboards you can join, so there's no problem belonging to a "Beginner" and a language specific one if you want.
Neat Statistics
I'm planning on adding some statistics, but other than "what languages did we see yesterday" does anyone have any ideas?
Languages Seen On Day 06
- JavaScript × 4
- Python × 2
- Clojure × 1
- Elixir × 1
- Java × 1
- Kotlin × 1
- PHP × 1
- Prolog × 1
- Swift × 1
Many thanks to special guest stat compiler Massimo Artizzu
Top comments (23)
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.
Roughly I think:
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.
Got the output. Its 3:30 AM and I can finally sleep now.
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.
My attempt:
In part 1, a sequence of 2 amps (A0,A1) will operate like so:
Part 2 is... complicated to diagram, but I will try (again with two amps, A5 and A6)
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!
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.
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.
Here's my part 2 solution in #haskell. I ended up needing to do some substantial refactoring from my previous solution that I'd used for day 5 as well as part 1. My original version attempting to go for performance by using mutable vectors to store the program state, ostensibly allowing for more performant seeks and writes to the program state. I started to extend this approach using IORefs to program state and some other clever trickery. Although I managed to get constant memory utilization, the performance was so bad the program never actually finished running.
After profiling didn't help enough, and I verified that there was nothing unsound about my algorithm, I decided to start from the ground up with the simplest solution and went back to a pure implementation using record updates to manage all of the changes to start, and manually sending IO between the amplifiers. Once again proving that when it comes to optimization I'm never as clever as I think I am, the compiler did it's job and took my naive implementation and gave me a version that finished in a couple hundred milliseconds.
My new version needs some refactoring still, both for readability and ergonomics during future challenges, but here is the version left on my disk from when I finally finished up at 5am:
Yoooo and I thought day 5 was annoying 😫
Anyway... JavaScript. It has this nice generator thing that I basically never used professionally and very rarely by myself, and here I am using them twice here. But you know, they're quite good for stateful machines. (Which is also why I rarely use them - like many others, I prefer stateless approaches.)
This is the solution for the second part, as the first can run the first (except for the phase settings, where I just add 5 to the permutation's elements - see at the end).
Many parts are the same of day 5 of course. Sorry for the repetition.
See text, input and first part on my repo.
Horray, finally got part 2 solved!! happy dance Took FOREVER but I finally got it working. This will be the part 2 solution in JavaScript (part 1 was very similar, just slightly more simplified and didn't involve the objects I created for each amp).
Day 7 with swift
I did it all in sync, runs well enough nonetheless.
And here's the ever growing intcode computer: github.com/jkoenig134/AdventOfCode...
Part one was a piece of cake imo. Part two was a lot tougher.
I decided to go with javascript and use generators so I could easily suspend the amplifier program between inputs
Well, today was definitely fun. Continuing in Python from previous days, part one was fairly easy as the modifications to my existing Intcode computer were minimal and the task itself was fairly straight-forward, but part two was interesting.
For part two, I chose to rewrite my whole Intcode computer in dart, as that is the language I am most familiar with, and I'm happy I did so. I then gave each amplifier a StreamController as its output and the BroadcastStream of the previous amplifier's StreamController as its input and had them listen to each other in async. Probably not the easiest way to do it, but very fun and interesting to write.
Here's the code for part 2:
And the code for my dart Intcode Computer: gist.github.com/jakcharvat/818c477...
Here's my solution to part 1 minus the obscene overhaul to Day05. I spent too long futzing because my refactor wasn't as friendly as I had hoped, and there was a terrible bug in my permutation generator.
I'm now mulling whether I want to go full Coroutines for part 2...