However, part 2 doesn't seem to be working. It never asks for input and I don't know why!I was accidentally saving state between runs. DOH!
Kotlin solution for part 1
importarrow.core.Eitherimportarrow.core.Noneimportarrow.core.Someimportarrow.core.leftimportarrow.core.rightimportarrow.optics.opticsimportintcode.CurrentStateimportintcode.IntCodeimportintcode.handleCodePointimportintcode.toIntCodeProgramimportjava.util.*@opticsdata classAscii(valcode:IntCode,valoutput:LinkedList<Long>=LinkedList(),valstate:Either<String,CurrentState>=CurrentState().right(),valinputs:LinkedList<Long>=LinkedList()){companionobject}privatetailrecfunAscii.runCode():Ascii{returnwhen(state){isEither.Left<String>->thisisEither.Right<CurrentState>->when(state.b.pointer){isNone->thisisSome<Long>->{valnAscii=processOutput().sendInput()nAscii.copy(state=handleCodePoint(code,nAscii.state)).runCode()}}}}privatefunAscii.sendInput():Ascii{returnstate.fold(ifLeft={this},ifRight={when{it.waitingForInput&&inputs.isEmpty()->copy(state="Error: Attempted to get input with no inputs left.".left())it.waitingForInput->{valnInstr=inputs.pop()it.inputs.add(nInstr.toLong())copy(state=it.right())}else->{this}}})}privatetailrecfunAscii.processOutput():Ascii{returnwhen(state){isEither.Left<String>->thisisEither.Right<CurrentState>->when{state.b.output.isEmpty()->thiselse->{valo=state.b.output.pop()when{o>127->println("NONASCII:$o")else->print(o.toChar())}output.add(o)if(o.toChar()=='\n'){output.clear()}processOutput()}}}}objectDay17{privateconstvalFILENAME="src/main/resources/day17.txt"valfileData=FILENAME.toIntCodeProgram()funpart1(){valascii=Ascii(code=fileData).runCode()println(ascii.output.joinToString(""){it.toChar().toString()})valpoints=ascii.output.joinToString(""){l->"${l.toChar()}"}.lines().mapIndexed{y,line->line.withIndex().filter{(_,c)->c=='#'}.map{(x,c)->Point(x,y)toc}}.flatten().toMap()valintersections=points.filterKeys{p->allDirections().map{d->p.inDirection(d)}.all(points::contains)}.keysprintln(intersections.sumBy{p->p.x*p.y})}valprog="A,B,A,C,B,C,B,C,A,B\n"+"L,6,L,4,R,8\n"+"R,8,L,6,L,4,L,10,R,8\n"+"L,4,R,4,L,4,R,8\n"+"y\n"funpart2(){println(prog.lines().map{it.length})println(prog.map{b->b.toLong()})valnewCode=FILENAME.toIntCodeProgram().apply{this[0]=2}valascii=Ascii(code=newCode,inputs=LinkedList(prog.map{it.toLong()}),state=CurrentState().right()).runCode()println(ascii)}}funmain(){Day17.part1()Day17.part2()}
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.
Woo! I got in the mid-900s for part 1!
However, part 2 doesn't seem to be working. It never asks for input and I don't know why!I was accidentally saving state between runs. DOH!Kotlin solution for part 1