Advent of Code 2019 - Day 5

I enjoyed this one a ton! I’m pretty happy with how my code turned out: https://gist.github.com/QuinnWilton/35d82ae4aab4fb39e2ac622ab54fc460

Here’s mine. Quite easy to solve with some updates upon day2’s solution.

3 Likes

I liked building the interpreter, but hopefully it was the last time we had to use it. :slight_smile:

@qhwa Slick solution. :+1:

1 Like

And I hope for more of them, hoping to be able to maintain compatibility from day 2 until day 25 (or whenever the last update is done)

Today I used a couple of techs learned from your last video, such as the combination of rem and div to get digits.

And interestingly, I found some common mistakes we both made, such as wrongly counting the pointer. :joy:
It was a long puzzle and bugs happened. I am still stuck in Y2015 Day22, passing all the tests yet not able to figure out the solution. :rofl:

Helpfully this time a lot of test codes were present, making things a lot easier.

So, thanks for sharing, again! Looking forward to more of it.

1 Like

Struggled a little with part2 of Day 5. Wrote a disassembler to see what was going on. Hopefully it will be useful for days ahead.

Disassembed Listing: input_05.txt
Program Size: 678
Entry Point: 0
Input List: 5
----------
  0: input   5
  2: add     5, 1100, 6
  6: jmpt    1, 238

238: jmpt    0, 99999
241: jmpt    227, 247

247: jmpt    0, 99999
250: jmpt    3, 256

256: jmpf    227, 99999
259: jmpf    0, 265

265: jmpf    3, 99999
268: jmpf    0, 274

274: jmpt    1, 280

280: add     5, 5, 225
284: add     294, 0, 0
288: jmpt    1, 294

294: jmpf    0, 300

300: add     10, 10, 225
304: add     314, 0, 0
308: jmpf    0, 314

314: cmpeq   226, 677, 224     ; 0 written
318: mul     2, 0, 223
322: jmpf    0, 329

329: cmpless 677, 226, 224     ; 0 written
333: mul     0, 2, 223
337: jmpf    0, 344

344: cmpeq   677, 226, 224     ; 0 written
348: mul     2, 0, 223
352: jmpf    0, 359

359: cmpless 226, 677, 224     ; 1 written
363: mul     2, 0, 223
367: jmpt    1, 374

374: cmpeq   677, 226, 224     ; 0 written
378: mul     0, 2, 223
382: jmpf    0, 389

389: cmpless 677, 677, 224     ; 0 written
393: mul     2, 0, 223
397: jmpf    0, 404

404: cmpless 226, 677, 224     ; 1 written
408: mul     0, 2, 223
412: jmpf    1, 419
415: add     0, 1, 223
419: cmpeq   677, 226, 224     ; 0 written
423: mul     2, 1, 223
427: jmpt    0, 434
430: add     2, 1, 223
434: cmpeq   226, 226, 224     ; 1 written
438: mul     3, 2, 223
442: jmpf    1, 449
445: add     6, 1, 223
449: cmpless 677, 226, 224     ; 0 written
453: mul     7, 2, 223
457: jmpf    0, 464

464: cmpless 226, 677, 224     ; 1 written
468: mul     2, 14, 223
472: jmpt    1, 479

479: cmpless 677, 226, 224     ; 0 written
483: mul     28, 2, 223
487: jmpt    0, 494
490: add     56, 1, 223
494: cmpeq   226, 677, 224     ; 0 written
498: mul     57, 2, 223
502: jmpt    0, 509
505: add     114, 1, 223
509: cmpless 677, 677, 224     ; 0 written
513: mul     115, 2, 223
517: jmpf    0, 524

524: cmpless 677, 677, 224     ; 0 written
528: mul     2, 230, 223
532: jmpt    0, 539
535: add     1, 460, 223
539: cmpless 677, 226, 224     ; 0 written
543: mul     2, 461, 223
547: jmpt    0, 554
550: add     922, 1, 223
554: cmpeq   677, 677, 224     ; 1 written
558: mul     923, 2, 223
562: jmpf    1, 569
565: add     1846, 1, 223
569: cmpeq   226, 677, 224     ; 0 written
573: mul     1847, 2, 223
577: jmpf    0, 584

584: cmpeq   226, 226, 224     ; 1 written
588: mul     3694, 2, 223
592: jmpf    1, 599
595: add     7388, 1, 223
599: cmpeq   677, 226, 224     ; 0 written
603: mul     2, 7389, 223
607: jmpf    0, 614

614: cmpless 226, 226, 224     ; 0 written
618: mul     14778, 2, 223
622: jmpf    0, 629

629: cmpeq   226, 677, 224     ; 0 written
633: mul     2, 29556, 223
637: jmpt    0, 644
640: add     1, 59112, 223
644: cmpless 226, 677, 224     ; 1 written
648: mul     59113, 2, 223
652: jmpt    1, 659

659: cmpeq   226, 226, 224     ; 1 written
663: mul     2, 118226, 223
667: jmpf    1, 674
670: add     236452, 1, 223
674: output  236453
676: stop    
----------
Outputs Written: 236453
5 Likes

I used Elixir for some problems but not day 2 and 5 as mutability seems to be very practical here.

I did not felt like creating a new memory structure for each write. I was tempted to use ETS though but it seemed overkill.

Tonight I took some time and tried to screencast myself while refactoring my intcode machine.

It is currently uploading to youtube and should be available soon:

Be aware though that it is 90 minutes of me beeing desoriented stroling through my own code talking with myself through cheap bluetooth headphones with an even cheaper microphone, because it was the only thing that I got to work which didn’t make more background noise than my voice in the foreground…

3 Likes

Catching up on this problem.

My Gleam solution

Wow, I really love your solution @qhwa. I was so focused on decomposing everything into small functions, but I think that you’ve demonstrated how that’s really an over-complication of this problem. I think that the amount of coupling and inter-dependency between the “components” are high enough here that defining functions locally with state in context is actually more elegant and simple than decomposition.

For comparison, this was my solution (a total refactor of my messier earlier attempt): https://github.com/montanonic/advent2019_elxir/blob/master/lib/day5.ex

I drew very heavily from Sasa’s solution after debugging older code for days. I hadn’t thought to store input and output data in a record along with the program instructions, and learned from his solution that the design benefits heavily from passing around a larger state record here. But I think again, what this really points at, is how interdependent the parts are on the overall state.

1 Like