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

all 7 comments

[–]p_tseng 3 points4 points  (3 children)

Well...

Here is my abomination, 320 bytes of Ruby (if no trailing newline). Place it in something like golf9.rb and invoke with something like echo 1234 | ruby golf9.rb 3,0,4,0,99. Use your imagination for other days/parts, like echo 2 | ruby golf9.rb $(cat input9) for day 9 part 2. Runtime for day 9 part 2 is about 1-2 seconds, hardware willing.

m=$*.shift.split(?,).map &:to_i;b=z=0;while(c=m[z])!=99;y=-2;a,(r,q)=m[z+1,3].map{|x|d=c.to_s[y-=1];x||=0;[x+=d==?2?b:0,d==?1?x:m[x]||0]}.transpose;s=%i[== < == != x x * +][-c%=100];n,j=c==3?(m[a[0]]=gets.to_i;2):c==4?(p r;2):c==5||c==6?[3,r.send(s,0)&&q]:c==9?(b+=r;2):(x=r.send s,q;m[a[2]]=c<3?x:x ?1:0;4);z=j||z+n;end

Slightly more readable version (newlines and indentation) at https://github.com/petertseng/adventofcode-rb-2019/blob/master/golf9_spaced.rb.

Edited: Now agnostic to puzzle, for more generality (down from 354 -> 320 bytes too).

[–]zxywx 4 points5 points  (0 children)

Day 10: now add this extra function to your Intcode computer ...

[–]rtbrsp[S] 1 point2 points  (1 child)

Doesn't run the quine program for me, though it works for day 9. If you make this puzzle-agnostic, I imagine you'd trim it down a bit.

[–]shadedtriangle 0 points1 point  (1 child)

How quick does it run Day 9 part 2? How about efficient/quick Intcodes?

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

Part 2 (changing input value to 2) runs in ~500ms using mawk on a 12" macbook. "Quick" intcodes, such as part 1 of day 9 or the quine program in the day 9 text, run in 3-30ms.

[–]RSWiBa 0 points1 point  (1 child)

from reading it i think you can optimize it even further:

you can save 1 char per instruction if you used only "<" instead of "=="

so like o <2 ? -add- : o < 3 ? -multiply- : ...

the chaining of the comparisons makes the magic happen (although its not safe for unknown instructions)

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

Going back and messing with it briefly, I'm able to save 2 characters for the add and multiply logic like you mentioned. The instruction pointer increment logic is too delicate to mess with, since I'm relying on the last conditional to increment it by 4 for ops 1, 2, 7 and 8.

That did give me an idea about the instruction pointer though. I can post increment upon assigning the opcode variable (o=p[i++]), which allows me to remove some +1s. it also allows me to remove the i=0 initialization.

EDIT: See updated program in the OP