File tree Expand file tree Collapse file tree 1 file changed +7
-10
lines changed Expand file tree Collapse file tree 1 file changed +7
-10
lines changed Original file line number Diff line number Diff line change 11import scala .io .Source
22
3- val pars = Source .fromFile(" input.txt" ).toList
3+ val pars = Source .fromFile(" input.txt" ).toStream
44
55def upOrDown (c : Char ) = if (c == '(' ) 1 else - 1
66
77val finalFloor = pars.foldLeft(0 )((floor, p) => floor + upOrDown(p))
88
9- def foo (floor : Int , seq : Int , pars : List [Char ]): Int = {
10- if (floor == - 1 ) seq
11- else pars match {
12- case h :: t => foo(floor + upOrDown(h), seq + 1 , t)
13- case Nil => sys.error(" Never reached basement" )
14- }
15- }
16-
17- val firstTimeInBasement = foo(0 , 0 , pars)
9+ val firstTimeInBasement = pars
10+ .map(upOrDown)
11+ .scanLeft(0 ){ case (floor, d) => floor + d }
12+ .zipWithIndex
13+ .filter(_._1 == - 1 )
14+ .head._2
1815
1916println(s " The instructions take santa to floor $finalFloor" )
2017println(s " The first time santa enters the basement is at instruction number $firstTimeInBasement" )
You can’t perform that action at this time.
0 commit comments