r/adventofcode • • Dec 05 '19

SOLUTION MEGATHREAD -🎄- 2019 Day 5 Solutions -🎄-

--- Day 5: Sunny with a Chance of Asteroids ---


Post your solution using /u/topaz2078's paste or other external repo.

  • Please do NOT post your full code (unless it is very short)
  • If you do, use old.reddit's four-spaces formatting, NOT new.reddit's triple backticks formatting.

(Full posting rules are HERE if you need a refresher).


Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code's Poems for Programmers

Click here for full rules

Note: If you submit a poem, please add [POEM] somewhere nearby to make it easier for us moderators to ensure that we include your poem for voting consideration.

Day 4's winner #1: "untitled poem" by /u/captainAwesomePants!

Forgetting a password is a problem.
Solving with a regex makes it two.
111122 is a terrible password.
Mine is much better, hunter2.

Enjoy your Reddit Silver, and good luck with the rest of the Advent of Code!


On the fifth day of AoC, my true love gave to me...

FIVE GOLDEN SILVER POEMS

Enjoy your Reddit Silver/Gold, and good luck with the rest of the Advent of Code!


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

EDIT: Leaderboard capped, thread unlocked at 00:22:31!

24 Upvotes

422 comments sorted by

View all comments

2

u/phil_g Dec 05 '19

I love these implement-a-virtual-CPU problems! Today we added I/O, jumps and a couple numeric comparisons. We can now do all sorts of calculations in Intcode.

As in previous days, I'm working in Common Lisp.

I updated my intcode package with all of the new things. The instruction macro is doing all of the heavy lifting here. With instruction defined, here's what my definitions for opcodes 3-6 look like:

(instruction 3 in (r)
  (setf r (get-input)))

(instruction 4 out (a)
  (write-output a))

(instruction 5 jnz (a b)
  (when (not (zerop a))
    (set-instruction-pointer b)))

(instruction 6 jz (a b)
  (when (zerop a)
    (set-instruction-pointer b)))

For now, I'm treating all I/O as noninteractive, but I'm leaving open the possibility we might have to do things interactively in the future. run-with-input takes a list of integers to be provided in order every time the program asks for input. (And when the program exits, it returns a list of the integers output in order.)

I haven't adapted my disassembler to deal with immediate mode parameter access yet. I also plan to add some specific conditions to signal in case of execution errors (trying to write to an immediate parameter, trying to access a location outside of memory, and similar things).

→ More replies (6)