I've been sending input and read output "manually", but it worked well enough for day 7. Interestingly, I learned something about ruby today: arrays are essentially unbounded! so i didn't have worry about the memory issue, I could just read/write to where in the array I wanted and if the array was too short, I'd just read out a nil instead of getting some form of IndexOutOfBoundsError or something.
I made this comment somewhere else in the thread, but the fact that opcode 09 could also work in relative mode, (and that we could write to addresses in relative mode as well) threw me off, I spent way too long trying to track that bug down.
classIntCodedefinitialize(instructions)@instructions=instructions@ptr=0@in_buff=[]@out_buff=[]@rel_base=0@blocked=falseenddefsend_input(thing)@in_buff.push(thing)enddefread_output()return@out_buff.shift()enddefinspect()puts@instructions.to_sputs@out_buff.to_senddefisBlocked()return@blockedenddefread_mem(idx)ifidx<0puts"wat: negative index"endval=@instructions[idx]ifval==nil@instructions[idx]=0return0elsereturnvalendenddefwrite_mem(idx)enddefrun()@blocked=falsewhile@ptr<@instructions.lengthdo# puts @ptr, @instructions.to_sbase_op=read_mem(@ptr)# puts base_opop=base_op%100# get last 2 digitsflags=base_op/100# top n digitsarg1=read_value(@instructions,@ptr+1,flags%10)ifop==1flags=flags/10arg2=read_value(@instructions,@ptr+2,flags%10)flags=flags/10arg3=read_value(@instructions,@ptr+3,flags%10)r=read_mem(arg1)+read_mem(arg2)@instructions[arg3]=r@ptr+=4elsifop==2flags=flags/10arg2=read_value(@instructions,@ptr+2,flags%10)flags=flags/10arg3=read_value(@instructions,@ptr+3,flags%10)r=read_mem(arg1)*read_mem(arg2)@instructions[arg3]=r@ptr+=4elsifop==3if@in_buff.empty?# puts "waiting for input"@blocked=truereturnfalseendinput=@in_buff.shift()# puts "got input #{input}, putting in location #{arg1}"@instructions[arg1]=input.to_i@ptr+=2elsifop==4output=read_mem(arg1)@out_buff.push(output)# puts "output: #{output}"@ptr+=2elsifop==5flags=flags/10arg2=read_value(@instructions,@ptr+2,flags%10)ifread_mem(arg1)!=0@ptr=read_mem(arg2)else@ptr+=3endelsifop==6flags=flags/10arg2=read_value(@instructions,@ptr+2,flags%10)ifread_mem(arg1)==0@ptr=read_mem(arg2)else@ptr+=3endelsifop==7flags=flags/10arg2=read_value(@instructions,@ptr+2,flags%10)flags=flags/10arg3=read_value(@instructions,@ptr+3,flags%10)ifread_mem(arg1)<read_mem(arg2)@instructions[arg3]=1else@instructions[arg3]=0end@ptr+=4elsifop==8flags=flags/10arg2=read_value(@instructions,@ptr+2,flags%10)flags=flags/10arg3=read_value(@instructions,@ptr+3,flags%10)ifread_mem(arg1)==read_mem(arg2)@instructions[arg3]=1else@instructions[arg3]=0end@ptr+=4elsifop==9@rel_base+=read_mem(arg1)# puts "updated relative base to #{@rel_base}"@ptr+=2elsifop==99# puts "halting!"breakelseputs"wat"return@instructionsendendreturn@instructionsenddefread_value(instructions,arg,flag)ifflag==1returnargelsifflag==2v=read_mem(arg)+@rel_basereturnvelsereturnread_mem(arg)endendendif__FILE__==$0instructions=[]File.open(ARGV[0],"r")do|file_handle|file_handle.each_linedo|line|instructions=line.split(",").map{|n|n.to_i}breakendendvm=IntCode.new(instructions)vm.send_input(2)vm.run()putsvm.read_output()end
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I've been sending input and read output "manually", but it worked well enough for day 7. Interestingly, I learned something about ruby today: arrays are essentially unbounded! so i didn't have worry about the memory issue, I could just read/write to where in the array I wanted and if the array was too short, I'd just read out a
nil
instead of getting some form ofIndexOutOfBoundsError
or something.I made this comment somewhere else in the thread, but the fact that opcode
09
could also work in relative mode, (and that we could write to addresses in relative mode as well) threw me off, I spent way too long trying to track that bug down.