Op 9-11-2018 om 16:44 schreef Peter Uhnak:


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

oke, I see how it works,
first you calculate all the floors Santa is visiting and then you look for the first -1.
I have thought of something but I found it wierd that you do steps when you have already the answer.

but how do I find now the answer do I have to do something like   puzzle1  stepChanges detectIndex ? 

Roelof


Reply via email to