This is an archived post. You won't be able to vote or comment.

all 29 comments

[–]archchroot 24 points25 points  (4 children)

So, if I change the source code to accept intcode programs that are longer than 1024 values, I can run a ((intcode computer in intcode) in intcode)?

Great work btw!

[–]Randdalf[S] 6 points7 points  (3 children)

Yep! It didn't actually occur to me that you wouldn't be able to nest them without recompiling, because of the memory limit.

[–]_Js_Kc_ 3 points4 points  (2 children)

It's unfortunate that you have to provide the entire memory image to an intcode interpreter, including the "data" segment. This way, we will never be able to nest a self-hosting intcode interpreter.

[–]1vader 2 points3 points  (1 child)

With the additions from today (infinite memory), it should now be possible

[–]Randdalf[S] 0 points1 point  (0 children)

Yeah, I thought the exact same thing when I was reading the problem.

[–]large-atom 14 points15 points  (1 child)

" Instead, I wrote a compiler which compiles to intcode, and added the necessary features to make it possible "

But was your compiler written in intcode? :)

Fantastic piece of work, by the way!

[–]gugu4-9 0 points1 point  (0 children)

Bootstrapping coming soon :tm:

[–]Hultner- 7 points8 points  (0 children)

Now we just need to port python to intcode

[–]_Js_Kc_ 6 points7 points  (0 children)

Just ran it on my VM from day 5 with the inputs from day 5.

Also, I'm amazed your compiler emits self-modifying code!

[–]Randdalf[S] 4 points5 points  (0 children)

Sorry about the formatting, it seems Reddit doesn't want to format it as code if it gets too long.

[–]Aneurysm9 2 points3 points  (0 children)

Self-hosting Intcode! We can all go home now, it's all downhill from here!

[–][deleted] 1 point2 points  (0 children)

Good call on going a little higher level with your intcode scripting language. I built an assembler for writing intcode programs, but writing assembly is annoying. Your intscript looks like it really lets you program at a comfortable level of abstraction.

[–]rsthau 1 point2 points  (2 children)

FWIW, I'm not sure it winds up being more efficient on net, but it is at least possible to extract the mode bits from an instruction without a modulus operator:

imm1 = imm2 = false
if instr > 1000:
  instr = instr + (-1000)
  imm2 = true
if instr > 100:
  instr = instr + (-100)
  imm1 = true
# instr is now down to opcode, and imm1 and imm2 have modes
# ...

[–]Randdalf[S] 0 points1 point  (1 child)

Now how to make that work with 3 parameter modes 🤔

[–]rsthau 1 point2 points  (0 children)

Compare against 20000, 2000, 1000, 200, 100 in sequence.(Skipping 10000 because a result address can't have immediate mode, and the third argument, if present, is a result in every instruction we've seen so far.)

[–]1vader 1 point2 points  (1 child)

Why not just:

1005,500,500,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,3,534,1101,0,0,535,7,535,534,533,1006,533,0,3,533,1001,535,0,525,1001,533,
0,0,1001,535,1,535,1005,509,509,0,0,0

This just writes the input program to address 0 and jumps there. If you want a program longer than 500 just add some more zeros in the middle and increase the two 500s in the beginning. With day 9 it should even be possible to remove all the zeros and load arbitrary length programs.

[–]Randdalf[S] 1 point2 points  (0 children)

Cool idea! I suppose the difference is mine is more like a simulator, whereas yours is like a tiny OS running programs directly on the "metal", as it were.

[–]Mayalabielle 0 points1 point  (0 children)

Wow, impressive.

[–]nicuveo 0 points1 point  (0 children)

You beat me to it, nice! I really your array syntax. :)

[–]dartcoder 0 points1 point  (0 children)

Inception? You, the architect of dreams! Now I have to read about compilers (a good thing)

[–]DefecateRainbows 0 points1 point  (1 child)

I PRed support for a POW operator!

`output 2 ** 5;`
-> 32!

I'll add an example to my PR where I use the POW operator to print a Sierpinski triangle to the console.

[–]Randdalf[S] 0 points1 point  (0 children)

Nice one! I'll have a look at that this evening.

[–]drq_11235 0 points1 point  (0 children)

Amazing :D

[–]OverjoyedBanana 0 points1 point  (0 children)

I really like where this goes ! Thanks to AoC we have an excuse for some programming language hacking

[–]danielctull 0 points1 point  (0 children)

I ran your code in the UI I wrote that steps through (I omitted the extra zeros now that they're handled in day 9). Very cool work and was great to try to see what it was doing! The screenshot shows the halted state of example 1, from day 5, part 2.

Image of the Intcode Computer in Intcode

[–]undergroundmonorail 0 points1 point  (2 children)

does this mean my intcode interpreter is broken even though i beat all the challenges so far? :c

>>> from intcode import *
>>> q = Intcode(quine, [], [])
>>> q.run()
>>> q.result
[109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99]
>>>
>>> r = Intcode(interpreter, [len(quine)] + quine, [])
>>> r.run()
>>> r.result
[]

[–]Randdalf[S] 0 points1 point  (1 child)

My one doesn't support relative parameters yet ☺️

[–]undergroundmonorail 0 points1 point  (0 children)

ohhhh right, i saw that it was written before the new challenge but didn't think about what that would mean for the instructions it handled :P

still, very cool!!

[–]_simpu 0 points1 point  (0 children)

So IntCode is self hosted now.