can I improve this

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
22 messages Options
12
Reply | Threaded
Open this post in threaded view
|

can I improve this

Roelof
Hello,

I have this code to solve the AOC 2018 day1  part1 challenge

| input input2|
input := Array streamContents: [ :lines |
  'input.txt' asFileReference readStreamDo: [ :in |
    [ in atEnd ] whileFalse: [ lines nextPut: in nextLine ]]].
input inject: 0 into: [ :sum :each | each asInteger + sum  ]

is there a way I can improve this code for example do the counting in the stream ? 

Roelof

Reply | Threaded
Open this post in threaded view
|

Re: can I improve this

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 | Threaded
Open this post in threaded view
|

Re: can I improve this

Roelof
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 | Threaded
Open this post in threaded view
|

Re: can I improve this

philippeback
In reply to this post by Richard O'Keefe
Also, given the time that some people solve the puzzle, doing it too nice is actually not the best use of time in AoC.

Now, I'll try to solve quick and this will for sure lead to interesting things in Pharo.

From Reddit, people solve it quick then make it nice.

Phil

On Mon, Dec 3, 2018, 03:45 Richard O'Keefe <[hidden email] wrote:
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 | Threaded
Open this post in threaded view
|

Re: can I improve this

Roelof
Hello,

Im not going for a quick answer.
For me its about practising and solving it and I do not matter to me how long it takes,
I have finisched the MOOC and see this as a opportunity to practice with Pharo ( the syntax and the way things can be solved)

Roelof



Op 3-12-2018 om 08:00 schreef [hidden email]:
Also, given the time that some people solve the puzzle, doing it too nice is actually not the best use of time in AoC.

Now, I'll try to solve quick and this will for sure lead to interesting things in Pharo.

From Reddit, people solve it quick then make it nice.

Phil

On Mon, Dec 3, 2018, 03:45 Richard O'Keefe <[hidden email] wrote:
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 | Threaded
Open this post in threaded view
|

Re: can I improve this

Richard O'Keefe
In reply to this post by Roelof
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 <[hidden email]> 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 | Threaded
Open this post in threaded view
|

Re: can I improve this

Roelof
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 <[hidden email]> 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 | Threaded
Open this post in threaded view
|

Re: can I improve this

Richard O'Keefe
"if I use do:   this ends at the end of the array."
True.  But all that means is that you have to keen on using #do:.

Processing an array repeatedly is as simple as
  [true] whileTrue: [
    anArray do: [:each |
      ...]]

I had intended to twist this into

  [changes anySatisfy: [:change | ...]] whileFalse.

However, in this case your second alternative has much to recommend it.
The control flow is simple.  There's no magic about it.  It's an
unusual setup, so no need to get fancy.

  seen := Set new.
  frequency := 0.
  found := false.
  i := changes size.
  [found] whileFalse: [
    i := i = changes size ifTrue: [1] ifFalse: [i+1].
    frequency := frequency + (changes at: i).
    found := seen includes: frequency.
    seen add: frequency].

I like this better than what I had.

If we _were_ to get fancy, then introducing a CyclicReadStream
class and doing
    seen := Set new.
    frequency := 0.
    found := false.
    stream := CyclicReadStream on: self changes.
    [found] whileFalse: [
      frequency := frequency + stream next.
      found := seen includes: frequency.
      seen add: frequency].
wouldn't shrink the code much.  Oddly enough, I've encountered
problems in previous Advent of Code exercises where CyclicReadStream
would have been handy.  But just for this case?  No.


On Tue, 4 Dec 2018 at 00:25, Roelof Wobben <[hidden email]> wrote:
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 <[hidden email]> 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 | Threaded
Open this post in threaded view
|

Re: can I improve this

Roelof
Thanks,

I found the function #atWrap  who maybe can be handy




Op 3-12-2018 om 13:17 schreef Richard O'Keefe:
"if I use do:   this ends at the end of the array."
True.  But all that means is that you have to keen on using #do:.

Processing an array repeatedly is as simple as
  [true] whileTrue: [
    anArray do: [:each |
      ...]]

I had intended to twist this into

  [changes anySatisfy: [:change | ...]] whileFalse.

However, in this case your second alternative has much to recommend it.
The control flow is simple.  There's no magic about it.  It's an
unusual setup, so no need to get fancy.

  seen := Set new.
  frequency := 0.
  found := false.
  i := changes size.
  [found] whileFalse: [
    i := i = changes size ifTrue: [1] ifFalse: [i+1].
    frequency := frequency + (changes at: i).
    found := seen includes: frequency.
    seen add: frequency].

I like this better than what I had.

If we _were_ to get fancy, then introducing a CyclicReadStream
class and doing
    seen := Set new.
    frequency := 0.
    found := false.
    stream := CyclicReadStream on: self changes.
    [found] whileFalse: [
      frequency := frequency + stream next.
      found := seen includes: frequency.
      seen add: frequency].
wouldn't shrink the code much.  Oddly enough, I've encountered
problems in previous Advent of Code exercises where CyclicReadStream
would have been handy.  But just for this case?  No.


On Tue, 4 Dec 2018 at 00:25, Roelof Wobben <[hidden email]> wrote:
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 <[hidden email]> 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 | Threaded
Open this post in threaded view
|

Re: can I improve this

Pharo Smalltalk Users mailing list
In reply to this post by philippeback
Concerning Day1, part2 of Advent Of Code 2018, here's my quick take (Coded in a workspace with Squeak)...


| file total numbers index frequencies duplicate |

duplicate := false.
frequencies := Set new: 150000.
frequencies add: 0.
numbers := OrderedCollection new: 1000.
total := 0.

file := StandardFileStream readOnlyFileNamed: 'day.1.input'.
[file atEnd] whileFalse: [numbers add: (Integer readFrom: file nextLine)].
file close.

index := 0.
[duplicate] whileFalse: [index := index \\ (numbers size) + 1.
                        total := total + numbers at: index.
                        (frequencies includes: total)
                            ifTrue: [duplicate := true]
                            ifFalse: [frequencies add: total]].

^total


-----------------
Benoît St-Jean
Yahoo! Messenger: bstjean
Twitter: @BenLeChialeux
Pinterest: benoitstjean
Instagram: Chef_Benito
IRC: lamneth
Blogue: endormitoire.wordpress.com
"A standpoint is an intellectual horizon of radius zero".  (A. Einstein)


On Monday, December 3, 2018, 2:01:08 a.m. EST, [hidden email] <[hidden email]> wrote:


Also, given the time that some people solve the puzzle, doing it too nice is actually not the best use of time in AoC.

Now, I'll try to solve quick and this will for sure lead to interesting things in Pharo.

From Reddit, people solve it quick then make it nice.

Phil

On Mon, Dec 3, 2018, 03:45 Richard O'Keefe <[hidden email] wrote:
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 | Threaded
Open this post in threaded view
|

Re: can I improve this

Roelof
Op 3-12-2018 om 15:30 schreef Benoit St-Jean via Pharo-users:

Thanks,

But at first glance you do not use the contents of file anywhere or am I
mistaken.
When I try your code on the real input I see a divide by zero error message.

Roelof



Reply | Threaded
Open this post in threaded view
|

Re: can I improve this

Roelof
yep, on  Pharo this is a empty collection.

numbers := OrderedCollection new: 1000.

so   index \\ (numbers size)   is  something divide by zero

Roelof



Op 3-12-2018 om 15:44 schreef Roelof Wobben:

> Op 3-12-2018 om 15:30 schreef Benoit St-Jean via Pharo-users:
>
> Thanks,
>
> But at first glance you do not use the contents of file anywhere or am
> I mistaken.
> When I try your code on the real input I see a divide by zero error
> message.
>
> Roelof
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: can I improve this

Pharo Smalltalk Users mailing list
Did you change the file name?

-----------------
Benoît St-Jean
Yahoo! Messenger: bstjean
Twitter: @BenLeChialeux
Pinterest: benoitstjean
Instagram: Chef_Benito
IRC: lamneth
Blogue: endormitoire.wordpress.com
"A standpoint is an intellectual horizon of radius zero".  (A. Einstein)


On Monday, December 3, 2018, 9:52:08 a.m. EST, Roelof Wobben <[hidden email]> wrote:


yep, on  Pharo this is a empty collection.

numbers := OrderedCollection new: 1000.

so   index \\ (numbers size)   is  something divide by zero

Roelof



Op 3-12-2018 om 15:44 schreef Roelof Wobben:

> Op 3-12-2018 om 15:30 schreef Benoit St-Jean via Pharo-users:
>
> Thanks,
>
> But at first glance you do not use the contents of file anywhere or am
> I mistaken.
> When I try your code on the real input I see a divide by zero error
> message.
>
> Roelof
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: can I improve this

Roelof
Op 3-12-2018 om 16:08 schreef Benoit St-Jean via Pharo-users:

yep

I did change it by the argument

partTwo: aFileName
     | total numbers index frequencies duplicate file |

duplicate := false.
frequencies := Set new: 150000.
frequencies add: 0.
numbers := OrderedCollection new: 1000.
total := 0.

file := self changesFrom: aFileName.

index := 0.
[duplicate] whileFalse: [index := index \\ (numbers size) + 1.
                         total := total + numbers at: index.
                         (frequencies includes: total)
                             ifTrue: [duplicate := true]
                             ifFalse: [frequencies add: total]].

^total

and changesFrom looks like this :

changesFrom: aFileName
     ^Array streamContents: [:changes |
       aFileName asFileReference readStreamDo: [:in |
         [in atEnd] whileFalse: [changes nextPut: in nextLine asInteger]]]


Reply | Threaded
Open this post in threaded view
|

Re: can I improve this

Pharo Smalltalk Users mailing list
I'm on Squeak btw.  Tried it in Pharo 6.1 and it looks like number parsing is different.

Here's a version that works fine on Pharo 6.1

| file total numbers index frequencies duplicate |

duplicate := false.
frequencies := Set new: 150000.
frequencies add: 0.
numbers := OrderedCollection new: 1000.
total := 0.

file := StandardFileStream readOnlyFileNamed: 'C:\Recv\day.1.input'.
[file atEnd] whileFalse: [numbers add: (file nextLine asInteger)].
file close.

index := 0.
[duplicate] whileFalse: [index := index \\ (numbers size) + 1.
                        total := total + numbers at: index.
                        (frequencies includes: total)
                            ifTrue: [duplicate := true]
                            ifFalse: [frequencies add: total]].

total


-----------------
Benoît St-Jean
Yahoo! Messenger: bstjean
Twitter: @BenLeChialeux
Pinterest: benoitstjean
Instagram: Chef_Benito
IRC: lamneth
Blogue: endormitoire.wordpress.com
"A standpoint is an intellectual horizon of radius zero".  (A. Einstein)


On Monday, December 3, 2018, 10:12:36 a.m. EST, Roelof Wobben <[hidden email]> wrote:


Op 3-12-2018 om 16:08 schreef Benoit St-Jean via Pharo-users:

yep

I did change it by the argument

partTwo: aFileName
    | total numbers index frequencies duplicate file |

duplicate := false.
frequencies := Set new: 150000.
frequencies add: 0.

numbers := OrderedCollection new: 1000.

total := 0.

file := self changesFrom: aFileName.

index := 0.
[duplicate] whileFalse: [index := index \\ (numbers size) + 1.
                        total := total + numbers at: index.
                        (frequencies includes: total)
                            ifTrue: [duplicate := true]
                            ifFalse: [frequencies add: total]].

^total

and changesFrom looks like this :

changesFrom: aFileName
    ^Array streamContents: [:changes |
      aFileName asFileReference readStreamDo: [:in |
        [in atEnd] whileFalse: [changes nextPut: in nextLine asInteger]]]



Reply | Threaded
Open this post in threaded view
|

Re: can I improve this

Roelof
Op 3-12-2018 om 16:13 schreef Benoit St-Jean via Pharo-users:
> file := StandardFileStream readOnlyFileNamed: 'C:\Recv\day.1.input'.
> [file atEnd] whileFalse: [numbers add: (file nextLine asInteger)].
> file close.
very wierd, on my Pharo 7 box I do not work.
I also see a message that temp variables are not written or read


Can you try your code on my input :
https://gist.github.com/RoelofWobben/001a0d7f9a8d5b5a0ebbc3f133dbd032
and tell me the outcome.

I have solved it already so you do not spoil anything


Roelof


Reply | Threaded
Open this post in threaded view
|

Re: can I improve this

Roelof
In reply to this post by Pharo Smalltalk Users mailing list
found it.

I use the old reading code from part1.
but on part2 you did add the input to the numbers collection

so numbers was keeping empty

sorry for the noise

Roelof



Op 3-12-2018 om 16:13 schreef Benoit St-Jean via Pharo-users:

> | file total numbers index frequencies duplicate |
>
> duplicate := false.
> frequencies := Set new: 150000.
> frequencies add: 0.
> numbers := OrderedCollection new: 1000.
> total := 0.
>
> file := StandardFileStream readOnlyFileNamed: 'C:\Recv\day.1.input'.
> [file atEnd] whileFalse: [numbers add: (file nextLine asInteger)].
> file close.
>
> index := 0.
> [duplicate] whileFalse: [index := index \\ (numbers size) + 1.
>                         total := total + numbers at: index.
>                         (frequencies includes: total)
>                             ifTrue: [duplicate := true]
>                             ifFalse: [frequencies add: total]].
>
> total


Reply | Threaded
Open this post in threaded view
|

Re: can I improve this

Pharo Smalltalk Users mailing list
In reply to this post by Roelof
Works fine with my Squeak and Pharo code.

Result is 558 in both cases!

-----------------
Benoît St-Jean
Yahoo! Messenger: bstjean
Twitter: @BenLeChialeux
Pinterest: benoitstjean
Instagram: Chef_Benito
IRC: lamneth
Blogue: endormitoire.wordpress.com
"A standpoint is an intellectual horizon of radius zero".  (A. Einstein)


On Monday, December 3, 2018, 10:37:57 a.m. EST, Roelof Wobben <[hidden email]> wrote:


Op 3-12-2018 om 16:13 schreef Benoit St-Jean via Pharo-users:
> file := StandardFileStream readOnlyFileNamed: 'C:\Recv\day.1.input'.
> [file atEnd] whileFalse: [numbers add: (file nextLine asInteger)].
> file close.
very wierd, on my Pharo 7 box I do not work.
I also see a message that temp variables are not written or read


Can you try your code on my input :
https://gist.github.com/RoelofWobben/001a0d7f9a8d5b5a0ebbc3f133dbd032
and tell me the outcome.

I have solved it already so you do not spoil anything



Roelof


Reply | Threaded
Open this post in threaded view
|

Re: can I improve this

Roelof
Op 3-12-2018 om 17:01 schreef Benoit St-Jean via Pharo-users:

Many many many thanks

For me too

and that is the answer I expect

Two question,

Why is trough StandardFileStream a line

and can this also be rewritten with a StreamContents



Roelof


Reply | Threaded
Open this post in threaded view
|

Re: can I improve this

Pharo Smalltalk Users mailing list
It's just a matter of taste and/or habit.  Quite frankly, I never think about using #streamContents nor #readStream or other variations since I've always had to write that kinda code "the long way" since 1992 !

-----------------
Benoît St-Jean
Yahoo! Messenger: bstjean
Twitter: @BenLeChialeux
Pinterest: benoitstjean
Instagram: Chef_Benito
IRC: lamneth
Blogue: endormitoire.wordpress.com
"A standpoint is an intellectual horizon of radius zero".  (A. Einstein)


On Monday, December 3, 2018, 11:09:31 a.m. EST, Roelof Wobben <[hidden email]> wrote:


Op 3-12-2018 om 17:01 schreef Benoit St-Jean via Pharo-users:

Many many many thanks

For me too

and that is the answer I expect

Two question,

Why is trough StandardFileStream a line

and can this also be rewritten with a StreamContents




Roelof


12