Kotlin solution! I had a lot of issues figuring out that I was off by one!
importarrow.core.Eitherimportarrow.core.Noneimportarrow.core.Someimportarrow.core.identityimportarrow.core.rightimportarrow.optics.opticsimportarrow.syntax.function.composeimportarrow.syntax.function.partially2importintcode.CurrentStateimportintcode.IntCodeimportintcode.handleCodePointimportintcode.toIntCodeProgramimportjava.util.*@opticsdata classDroneController(valcode:IntCode,valstate:Either<String,CurrentState>=CurrentState().right()){companionobject}privatetailrecfunDroneController.runCode():DroneController{returnwhen(state){isEither.Left<String>->thisisEither.Right<CurrentState>->when(state.b.pointer){isNone->thisisSome<Long>->{copy(state=handleCodePoint(code,state)).runCode()}}}}objectDay19{privateconstvalFILENAME="src/main/resources/day19.txt"valfileDataget()=FILENAME.toIntCodeProgram()valcache:MutableMap<PointL,Long>=mutableMapOf()funfindPowerAt(p:PointL):Long{returncache.getOrPut(p,{DroneController(code=fileData.toMutableMap(),state=CurrentState(inputs=LinkedList(listOf(p.x,p.y))).right()).runCode().state.fold({error("Unknonwn Problem at $p")},{state->state.output.pop()})})}funfourCorners(point:PointL,squareSize:Long)=listOf(::identity,PointL.x::modify::partially2{it+squareSize},PointL.x::modify::partially2{it+squareSize}composePointL.y::modify::partially2{it-squareSize},PointL.y::modify::partially2{it-squareSize}).all{findPowerAt(it(point))==1L}funpart1(){cache.clear()println((0until50).flatMap{y->(0until50).map{x->PointL(x.toLong(),y.toLong())}}.map(::findPowerAt).count{it==1L})}funpart2(){valsquareSize=99Lcache.clear()valn=generateSequence(squareSize){it+1}.map{y->generateSequence(y*0.7){it+1}.takeWhile{it<=y}.map{x->PointL(x.toLong(),y.toLong())}.dropWhile{findPowerAt(it)==0L}.first()}.dropWhile{!fourCorners(it,squareSize)}.first()println(n.x*10000+n.y-squareSize)}}funmain(){Day19.part1()Day19.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.
Kotlin solution! I had a lot of issues figuring out that I was off by one!