On Fri, 9 Nov 2018 at 15:25, Roelof Wobben <r.wob...@home.nl> wrote: > Hello, > > I try to solve a adventofcode challenge where I must find out when santa > is first at the basement. the floor is there -1. > So I thought I use a while loop like this : > > santaFloorOnBasement > "calculates which step take Santa to the basement" > | index| > index := 1. > ??? when: (floor >=0 ) do: > [ floor := (input at: index = '(' ) > ifTrue: [ floor + 1 ] > ifFalse: [ floor - 1 ]. > index := index + 1 ]. > > but I cannot find out what must be instead of the ?? >
I've never used #when:do: so I can't comment on that. I'd be using #whileTrue: which looks like this... [ condition block ] whileTrue: [ action block ] <<< ??? when: (floor >=0 ) do: >>> [floor>=0] whileTrue: For further examples review the "senders" of #whileTrue: i.e. highlight then press <CTRL-N> on MS Windows > index is the index of the string which has to start with a 1. > floor and input are both instance variables. floor contains the current > floor and input the input of the challenge. > > Can someone help me figure this out so the next time I can do this on my > own. > btw you have a problem here... floor := (input at: index = '(' ) Its not doing what you think and you'll get "Error: only integers should be used as indices" Consider you that two messages are being sent: * the keyword message... #at: * the binary message.... #= Homework :)... what is the third type of message and the evaluation priority of all three. Homework 2... Review the "implementors" of message #= <CTRL-M> on MS Windows cheers -ben P.S. As it current stands, you'll get a "SubscriptOutOfBounds" error if the input ends before Santa gets to the basement. An exercise, fix that using the existing loop. Then consider using an iterator... input do: [ :bracket | (bracket = $( ) ifTrue: [ floor := floor + 1 ] . (bracket = $( ) ifTrue: [ floor := floor + 1 ] ]. and maybe something else for you to experiment with... movement := Dictionary new. movement at: $( put: 1. movement at: $) put: -1. input do: [ :char | floor := floor + (movement at: char ifAbsent: [0]) ]. cheers -ben