hello Richard,

Thanks, I figured that out already.
What I do not get is how to read the array multiple times.

if I use do:   this ends at the end of the array.

Or I must use something as this :

index := 0
if index > array length
    index =:  1
else
index := index + 1


and then read the number with array at: index

Roelof


Op 3-12-2018 om 10:08 schreef Richard O'Keefe:
Roelof Wobben wrote
"I have to reread the file till the adding causes the same outcome  as we had already"
You have to process the *sequence of changes* repeatedly;
you DON't have to *reread the file*.  Somebody already made this point.
Having read the changes into an array, you can iterate repeatedly over that array.

to find the first repeated frequency given a sequence of changes
    make an empty Set to hold the sums that have been seen so far.
    set the frequency to 0.
    loop forever
        for each change in the changes
            increment the frequency by the change.
            if the frequency is in the set
                return the frequency
           otherwise add the frequency to the set.

 partTwo: aFileName
    Transcript print: (self firstRepeatedFrequency: (self changesFrom: aFileName)); cr; flush.

On Mon, 3 Dec 2018 at 19:31, Roelof Wobben <r.wob...@home.nl> wrote:
Thanks,

For the second I have to take a good look.

I have to reread the file till the adding causes the same outcome  as we had already

so for example if we have the sequence :

+3, +3, +4, -2, -4

it has as outcome :

3 6 10 8 4

so no outcome is there a second time so we repeat the sequence


7 10

the 10 shows up a second time so there we have our answer.


Roelof



Op 3-12-2018 om 03:45 schreef Richard O'Keefe:
The key question is "what do you mean by improve"?
I'd start by asking "what are you doing that you will still have to do in
part 2, and what won't you do?"  So looking at part 2, you will want to
convert the lines to integers, and
  input := Array streamContents: [:lines |
    'input.txt' asFileReference readStreamDo: [:in |
      [in atEnd] whileFalse: [lines nextPut: in nextLine asInteger]]].
gives you a chunk of code you can use in both parts.  So you might
want to have

Day1
  changesFrom: aFileName
    ^Array streamContents: [:changes |
      aFileName asFileReference readStreamDo: [:in |
        [in atEnd] whileFalse: [changes nextPut: in nextLine asInteger]]]
  partOne: aFileName
    ^(self changesFrom: aFileName) sum
  partTwo: aFileName
    ...
The file name should not be wired in because you want some test files.







Reply via email to