On Fri, Nov 9, 2018 at 1:26 PM Roelof Wobben <r.wob...@home.nl> wrote:

> Thanks all.
>
> This code seems to do the job
>
> santaFloorOnBasement
>     "calculates which step take Santa to the basement"
>      | index|
>      index := 1.
>      [floor ~= -1 ] whileTrue:
>         [ floor := ((input at: index) = '(' )
>                 ifTrue: [ floor + 1 ]
>                 ifFalse: [ floor - 1 ].
>          index := index + 1 ].
>
>     ^ index -1
>


You can also think of it from the perspective of data processing and not
instruction coding (and mangling of indexes).

stepChanges := input collect: [ :c |
floor := floor + (c = '(' ifTrue: [ 1 ] ifFalse: [ -1 ]).
floor
].

stepChanges detectIndex: [ :each | each = -1 ] ifNone: [ 'invalid input yo'
].

Peter

Reply via email to