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.
importjava.util.Scannerimportscala.annotation.tailrectraitTree{defvalue:BooleandefapplyRule(rule:Int,parentValue:Boolean=false):Tree@tailrecfinaldefchangeState(rule:Int,stepCount:Int):Tree=if(stepCount==0)thiselseapplyRule(rule).changeState(rule,stepCount-1)defatPath(path:List[Char]):BooleanprotecteddefnextValue(rule:Int,parentValue:Boolean,leftValue:Boolean,rightValue:Boolean):Boolean={deftoBit(value:Boolean,index:Int)=(if(value)1else0)<<indexvalbit=toBit(parentValue,3)|toBit(leftValue,2)|toBit(value,1)|toBit(rightValue,0)(rule&(1<<bit))!=0}}objectTree{defparse(s:List[Char]):Tree={definner(s:List[Char]):(Tree,List[Char])=smatch{caseNil=>(Empty,Nil)casec::cs=>cmatch{case'.'=>(Leaf(false),cs)case'X'=>(Leaf(true),cs)case'('=>val(left,afterLeft)=inner(cs)val(root,afterRoot)=inner(afterLeft)val(right,afterRight)=inner(afterRoot)(Node(root.value,left,right),afterRight)case_=>inner(cs)}}inner(s)._1}}caseclassNode(value:Boolean,left:Tree,right:Tree)extendsTree{overridedefapplyRule(rule:Int,parentValue:Boolean):Tree=Node(nextValue(rule,parentValue,left.value,right.value),left.applyRule(rule,value),right.applyRule(rule,value))overridedefatPath(path:List[Char]):Boolean=pathmatch{case']'::_=>valuecasec::cs=>(if(c=='<')leftelseright).atPath(cs)caseNil=>thrownewException("Wrong path")}}caseclassLeaf(value:Boolean)extendsTree{overridedefapplyRule(rule:Int,parentValue:Boolean):Tree=Leaf(nextValue(rule,parentValue,leftValue=false,rightValue=false))overridedefatPath(path:List[Char]):Boolean=pathmatch{case']'::_=>valuecase_=>thrownewException("Wrong path")}}objectEmptyextendsTree{overridedefvalue:Boolean=thrownewException("Value of empty tree")overridedefapplyRule(rule:Int,parentValue:Boolean):Tree=thisoverridedefatPath(path:List[Char]):Boolean=thrownewException("atPath of empty tree")}caseclassRule(n:Int)objectSolution{defmain(args:Array[String]):Unit={valsc=newScanner(System.in)valrule=sc.nextIntsc.nextLinevals=sc.nextLinevalroot=Tree.parse(s.toList)valn=sc.nextIntcaseclassQuery(initialIndex:Int,time:Int,path:List[Char])@scala.annotation.tailrecdefreadQueries(rest:Int,time:Int,acc:List[Query]):List[Query]=if(rest==0)accelse{valstepCount=sc.nextIntvalpath=sc.nextLine.toList.drop(2)valnextTime=time+stepCountreadQueries(rest-1,nextTime,Query(n-rest,nextTime,path)::acc)}valqueries=readQueries(n,0,Nil).sortBy(_.time)sc.close()caseclassAnswer(initialIndex:Int,value:Boolean)caseclassAccumulator(tree:Tree,time:Int,answers:List[Answer])valaccumulator=queries.foldLeft(Accumulator(root,0,Nil))((acc,query)=>{valtree=acc.tree.changeState(rule,query.time-acc.time)Accumulator(tree,query.time,Answer(query.initialIndex,tree.atPath(query.path))::acc.answers)})println(accumulator.answers.sortBy(_.initialIndex).map(v=>if(v.value)'X'else'.').mkString("\n"))}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Tree Of Life
You are viewing a single comment's thread. Return to all comments →
incase anyone was looking for solution :