We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
traitCommand{defcall(machine:Machine):Machinedefcheck(machine:Machine,f:Machine=>Machine):Machine={valm=machine.copy(count=machine.count+1)if(machine.count<Program.maxCommandCount)f(m)elsem}}objectCommand{valbrainChars="><+-.,[]"defparse(text:List[Char]):(List[Command],List[Char])=textmatch{caseNil=>(Nil,Nil)casec::cs=>val(command,rest)=cmatch{case'>'=>(Greater(),cs)case'<'=>(Less(),cs)case'+'=>(Plus(),cs)case'-'=>(Minus(),cs)case'.'=>(Point(),cs)case','=>(Comma(),cs)case'['=>val(commands,rest)=parse(cs)(Open(commands),rest)case']'=>(Close(),cs)}commandmatch{case_:Close=>(command::Nil,rest)case_=>val(list,restOfRest)=parse(rest)(command::list,restOfRest)}}}caseclassGreater()extendsCommand{overridedefcall(machine:Machine):Machine=check(machine,m=>m.copy(cursor=m.cursor+1))}caseclassLess()extendsCommand{overridedefcall(machine:Machine):Machine=check(machine,m=>m.copy(cursor=m.cursor-1))}caseclassPlus()extendsCommand{overridedefcall(machine:Machine):Machine=check(machine,m=>{m.memory.update(m.cursor,(m.memory(m.cursor)+1).toByte)m})}caseclassMinus()extendsCommand{overridedefcall(machine:Machine):Machine=check(machine,m=>{m.memory.update(m.cursor,(m.memory(m.cursor)-1).toByte)m})}caseclassPoint()extendsCommand{overridedefcall(machine:Machine):Machine=check(machine,m=>{print(m.memory(m.cursor).toChar)m})}caseclassComma()extendsCommand{overridedefcall(machine:Machine):Machine=check(machine,m=>{m.memory(m.cursor)=m.input.headm.copy(input=m.input.tail)})}caseclassClose()extendsCommand{overridedefcall(machine:Machine):Machine=check(machine,m=>m.copy(count=m.count+(if(m.memory(m.cursor)==0)-2else0)))}traitCommandSequenceextendsCommand{defcommands:List[Command]=Nil}caseclassOpen(overridevalcommands:List[Command])extendsCommandSequence{@scala.annotation.tailrecfinaloverridedefcall(machine:Machine):Machine={valm=machine.copy(count=machine.count+1)if(machine.count<Program.maxCommandCount){if(m.memory(m.cursor)==0)m.copy(count=m.count+1)elsecall(commands.foldLeft(m)((innerMachine,command)=>command.call(innerMachine)))}elsem}}classProgram(text:List[Char])extendsCommandSequence{overridevalcommands:List[Command]=Command.parse(text)._1overridedefcall(machine:Machine):Machine=commands.foldLeft(machine)((m,command)=>command.call(m))}objectProgram{valmaxCommandCount=100000}caseclassMachine(cursor:Int,input:Seq[Byte],memory:Array[Byte],count:Int)objectSolution{defmain(args:Array[String]):Unit={valsc=newjava.util.Scanner(System.in)val(_,m)=(sc.nextInt,sc.nextInt)sc.nextLine()valinput=sc.nextLine.map(c=>c.toByte)valtext=(0untilm).flatMap(_=>sc.nextLine.filter(c=>Command.brainChars.contains(c))).toListvalprogram=newProgram(text)valmachine=program.call(Machine(0,input,newArray[Byte](Program.maxCommandCount),0))if(machine.count>Program.maxCommandCount){printlnprintln("PROCESS TIME OUT. KILLED!!!")}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
BrainF__k interpreter
You are viewing a single comment's thread. Return to all comments →