classOpcode{varnumbers:[Int]varinputIds:[Int]=[]varinputId:Intvardone=falsevarindex=0varrelativeBase=0varoutput:Int!varoutputs:[Int]=[]init(_input:[Int],_inputId:Int=0){self.numbers=inputnumbers.append(contentsOf:Array.init(repeating:0,count:1000))self.inputId=inputIdself.inputIds.append(inputId)}funcrun()->Int{guarddone==falseelse{returnoutput}whiletrue{letdigits=numbers[index]letopCode=((digits.digits.dropLast().last??0)*10)+digits.digits.last!varinstruction=digits.digits//print("Numbers \(numbers)")//print("Running opcode \(instruction)")funcgetValue(_aIndex:Int,_mode:Int=0)->Int{switchmode{case0:returnnumbers[numbers[index+1+aIndex]];case1:returnnumbers[index+1+aIndex];case2:returnnumbers[safe:relativeBase+numbers[index+1+aIndex]]??0;default:fatalError("unexpected mode \(mode)")}}funcsetValue(_index:Int,_value:Int,_mode:Int=0){//print("set index is : \(index) value s \(value) mode is \(mode)")switchmode{case0:numbers[index]=value//print("set index is : \(index)")case2://print(relativeBase + index)numbers[relativeBase+index]=valuedefault:break}}funcgetParam(_aIndex:Int)->Int{ifletmode=instruction.dropLast(2+aIndex).last{//print("mode is : \(mode)")//print("value is : \(getValue(aIndex,mode))")returngetValue(aIndex,mode)}returngetValue(aIndex)}funcsetParam(_aIndex:Int,_value:Int){//print("set value is : \(value)")//print("setting apram \(aIndex))")ifletmode=instruction.dropLast(2+aIndex).last{setValue(aIndex,value,mode)return}setValue(aIndex,value)}funcgetMode(_aIndex:Int)->Int{//print(instruction)ifletmode=instruction.dropLast(2+aIndex).last{returnmode}return0}switchopCode{case1:letp1=getParam(0)letp2=getParam(1)letp3=numbers[index+3]setValue(p3,p1+p2,getMode(2))index+=4case2:letp1=getParam(0)letp2=getParam(1)letp3=numbers[index+3]setValue(p3,p1*p2,getMode(2))index+=4case3:letp1=numbers[index+1]setValue(p1,inputIds.last!,getMode(2))// setParam(p1, inputIds.last!)index+=2case4:letp1=getParam(0)output=p1//print("output is :\(p1)")index+=2returnoutputcase5:letp1=getParam(0)letp2=getParam(1)ifp1!=0{index=p2}else{index+=3}case6:letp1=getParam(0)letp2=getParam(1)ifp1==0{index=p2}else{index+=3}case7:letp1=getParam(0)letp2=getParam(1)letp3=numbers[index+3]setValue(p3,p1<p2?1:0,getMode(2))index+=4case8:letp1=getParam(0)letp2=getParam(1)letp3=numbers[index+3]setValue(p3,p1==p2?1:0,getMode(2))index+=4case9:letp1=getParam(0)relativeBase+=p1index+=2// print("New R Base \(relativeBase)")case99:done=truereturnoutputdefault:fatalError("Unknown opcode \(opCode)")}}}}enumDirection:Int{caseup=0caseleft=1caseright=2casedown=3mutatingfuncturn(_direction:Int){ifdirection==0{switchself{case.up:self=Direction.leftcase.down:self=Direction.rightcase.left:self=Direction.downcase.right:self=Direction.up}}elseifdirection==1{switchself{case.up:self=Direction.rightcase.down:self=Direction.leftcase.left:self=Direction.upcase.right:self=Direction.down}}}}structPoint:CustomStringConvertible,Equatable,Hashable{varx:Intvary:Intvardescription:String{get{return"X: \(self.x) Y: \(self.y)"}}mutatingfuncmove(indirection:Direction){switchdirection{case.up:self.y+=1case.right:self.x+=1case.down:self.y-=1case.left:self.x-=1}}}extensionArraywhereElement:Hashable{varunique:[Element]{returnArray(Set(self))}}enumColor:Int{caseblack=0casewhite=1}classRobot{letstartColor:Colorvarpaint:[Point:Int]=[:]init(_startColor:Color){self.startColor=startColor}@discardableResultfuncrun()->Int{letcomputer=Opcode(input,startColor.rawValue)vardirection:Direction=.upvarcurrentPosition=Point(x:0,y:0)paint=[currentPosition:0]while!computer.done{letcolor=computer.run()letturn=computer.run()paint[currentPosition]=colordirection.turn(turn)currentPosition.move(in:direction)computer.inputIds.append(paint[currentPosition]??0)}returnSet(paint.keys).count}funcpaintHull(){letminX=paint.keys.min{a,bina.x<b.x}!.xletminY=paint.keys.min{a,bina.y<b.y}!.yletmaxX=paint.keys.min{a,bina.x>b.x}!.xletmaxY=paint.keys.min{a,bina.y>b.y}!.yvarline:[[String]]=[]foryin(minY..<maxY+1){varrow:[String]=[]forxin(minX..<maxX+1){letpoint=Point.init(x:x,y:y)letchar=paint[point]row.append(char==Color.white.rawValue?"#":" ")}line.insert(row,at:0)}foraLineinline{print(aLine,separator:"")}}}funcpartOne(){letresult=Robot(.black).run()print("Part 1 answer is :\(result)")}funcpartTwo(){letrobot=Robot(.white)robot.run()robot.paintHull()}partOne()partTwo()
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.
bit late but here you go Swift solution