why is masses not found?

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

why is masses not found?

Pharo Smalltalk Users mailing list
Hello,

Im still trying to make part1 of day2 working at a way I can also test
things.
the tests are working but when I call on the class side the method which
should reed the masses which are on the instanc side , the masses
cannnot be found.

So question 1 is why is masses not found.

and question 2  is how can I use the changed array at the process method
so I can calculate the outcome.

Regards,

Roelof


IntComputer.st (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

jtuchel
I only skimmed over your code briefly. It seems you implemented a class
method #masses which returns stuff, but never call that method. You
reference the class var #masses which is never initialized.

So I guess there should be some place where you assign the return value
of #masses to the masses variable....?

HTH

Joachim



Am 31.12.19 um 12:00 schrieb Roelof Wobben:

> Hello,
>
> Im still trying to make part1 of day2 working at a way I can also test
> things.
> the tests are working but when I call on the class side the method
> which should reed the masses which are on the instanc side , the
> masses cannnot be found.
>
> So question 1 is why is masses not found.
>
> and question 2  is how can I use the changed array at the process
> method so I can calculate the outcome.
>
> Regards,
>
> Roelof
>

--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1



Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Pharo Smalltalk Users mailing list
Thanks.

I think I misunderstood you.

There is a instance method called masses where all the numbers are
mentioned.
The class variable masses that I used there was just because masses were
not understood but that one can be deleted.

Roelof



Op 31-12-2019 om 12:18 schreef [hidden email]:

> I only skimmed over your code briefly. It seems you implemented a
> class method #masses which returns stuff, but never call that method.
> You reference the class var #masses which is never initialized.
>
> So I guess there should be some place where you assign the return
> value of #masses to the masses variable....?
>
> HTH
>
> Joachim
>
>
>
> Am 31.12.19 um 12:00 schrieb Roelof Wobben:
>> Hello,
>>
>> Im still trying to make part1 of day2 working at a way I can also
>> test things.
>> the tests are working but when I call on the class side the method
>> which should reed the masses which are on the instanc side , the
>> masses cannnot be found.
>>
>> So question 1 is why is masses not found.
>>
>> and question 2  is how can I use the changed array at the process
>> method so I can calculate the outcome.
>>
>> Regards,
>>
>> Roelof
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

jtuchel
In reply to this post by jtuchel
Roelof,

I didn't import your code, just read the .st file in an editor, and I'm
not an expert in Pharo source files, so maybe I overlooked something. I
even don't have Pharo on this machine I'm sitting at.
Still you seem to reference a variable called masses and send this as an
argument to readRam. But I cannot find a method that would initialize
this variable. Maybe what you wanted to write is

readRam: self masses

???
Without the self keyword, you are referencing some variable instead of
sending a message. Is that your problem?

Joachim


Am 31.12.19 um 12:26 schrieb Roelof Wobben:

> Thanks.
>
> I think I misunderstood you.
>
> There is a instance method called masses where all the numbers are
> mentioned.
> The class variable masses that I used there was just because masses
> were not understood but that one can be deleted.
>
> Roelof
>
>
>
> Op 31-12-2019 om 12:18 schreef [hidden email]:
>> I only skimmed over your code briefly. It seems you implemented a
>> class method #masses which returns stuff, but never call that method.
>> You reference the class var #masses which is never initialized.
>>
>> So I guess there should be some place where you assign the return
>> value of #masses to the masses variable....?
>>
>> HTH
>>
>> Joachim
>>
>>
>>
>> Am 31.12.19 um 12:00 schrieb Roelof Wobben:
>>> Hello,
>>>
>>> Im still trying to make part1 of day2 working at a way I can also
>>> test things.
>>> the tests are working but when I call on the class side the method
>>> which should reed the masses which are on the instanc side , the
>>> masses cannnot be found.
>>>
>>> So question 1 is why is masses not found.
>>>
>>> and question 2  is how can I use the changed array at the process
>>> method so I can calculate the outcome.
>>>
>>> Regards,
>>>
>>> Roelof
>>>
>>
>
>
>

--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1



Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Pharo Smalltalk Users mailing list
nope,

Then masses is still not understood , I tried that already and tried it
a few moments ago.

Roelof



Op 31-12-2019 om 12:30 schreef [hidden email]:

> Roelof,
>
> I didn't import your code, just read the .st file in an editor, and
> I'm not an expert in Pharo source files, so maybe I overlooked
> something. I even don't have Pharo on this machine I'm sitting at.
> Still you seem to reference a variable called masses and send this as
> an argument to readRam. But I cannot find a method that would
> initialize this variable. Maybe what you wanted to write is
>
> readRam: self masses
>
> ???
> Without the self keyword, you are referencing some variable instead of
> sending a message. Is that your problem?
>
> Joachim
>
>
> Am 31.12.19 um 12:26 schrieb Roelof Wobben:
>> Thanks.
>>
>> I think I misunderstood you.
>>
>> There is a instance method called masses where all the numbers are
>> mentioned.
>> The class variable masses that I used there was just because masses
>> were not understood but that one can be deleted.
>>
>> Roelof
>>
>>
>>
>> Op 31-12-2019 om 12:18 schreef [hidden email]:
>>> I only skimmed over your code briefly. It seems you implemented a
>>> class method #masses which returns stuff, but never call that
>>> method. You reference the class var #masses which is never initialized.
>>>
>>> So I guess there should be some place where you assign the return
>>> value of #masses to the masses variable....?
>>>
>>> HTH
>>>
>>> Joachim
>>>
>>>
>>>
>>> Am 31.12.19 um 12:00 schrieb Roelof Wobben:
>>>> Hello,
>>>>
>>>> Im still trying to make part1 of day2 working at a way I can also
>>>> test things.
>>>> the tests are working but when I call on the class side the method
>>>> which should reed the masses which are on the instanc side , the
>>>> masses cannnot be found.
>>>>
>>>> So question 1 is why is masses not found.
>>>>
>>>> and question 2  is how can I use the changed array at the process
>>>> method so I can calculate the outcome.
>>>>
>>>> Regards,
>>>>
>>>> Roelof
>>>>
>>>
>>
>>
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Pharo Smalltalk Users mailing list
In reply to this post by jtuchel
I solved it but I think with ugly code on the class side.

solution
     | computer |
     computer := self new.
     computer readRam: computer masses.
     computer patchRam: 1 value: 12.
     computer patchRam: 2 value: 2.
     computer processData: computer ram.
     computer at: 0

Is there a way I can make this more smalltalk.

Roelof




Op 31-12-2019 om 12:36 schreef Roelof Wobben:

> nope,
>
> Then masses is still not understood , I tried that already and tried
> it a few moments ago.
>
> Roelof
>
>
>
> Op 31-12-2019 om 12:30 schreef [hidden email]:
>> Roelof,
>>
>> I didn't import your code, just read the .st file in an editor, and
>> I'm not an expert in Pharo source files, so maybe I overlooked
>> something. I even don't have Pharo on this machine I'm sitting at.
>> Still you seem to reference a variable called masses and send this as
>> an argument to readRam. But I cannot find a method that would
>> initialize this variable. Maybe what you wanted to write is
>>
>> readRam: self masses
>>
>> ???
>> Without the self keyword, you are referencing some variable instead
>> of sending a message. Is that your problem?
>>
>> Joachim
>>
>>
>> Am 31.12.19 um 12:26 schrieb Roelof Wobben:
>>> Thanks.
>>>
>>> I think I misunderstood you.
>>>
>>> There is a instance method called masses where all the numbers are
>>> mentioned.
>>> The class variable masses that I used there was just because masses
>>> were not understood but that one can be deleted.
>>>
>>> Roelof
>>>
>>>
>>>
>>> Op 31-12-2019 om 12:18 schreef [hidden email]:
>>>> I only skimmed over your code briefly. It seems you implemented a
>>>> class method #masses which returns stuff, but never call that
>>>> method. You reference the class var #masses which is never
>>>> initialized.
>>>>
>>>> So I guess there should be some place where you assign the return
>>>> value of #masses to the masses variable....?
>>>>
>>>> HTH
>>>>
>>>> Joachim
>>>>
>>>>
>>>>
>>>> Am 31.12.19 um 12:00 schrieb Roelof Wobben:
>>>>> Hello,
>>>>>
>>>>> Im still trying to make part1 of day2 working at a way I can also
>>>>> test things.
>>>>> the tests are working but when I call on the class side the method
>>>>> which should reed the masses which are on the instanc side , the
>>>>> masses cannnot be found.
>>>>>
>>>>> So question 1 is why is masses not found.
>>>>>
>>>>> and question 2  is how can I use the changed array at the process
>>>>> method so I can calculate the outcome.
>>>>>
>>>>> Regards,
>>>>>
>>>>> Roelof
>>>>>
>>>>
>>>
>>>
>>>
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Ben Coman
In reply to this post by Pharo Smalltalk Users mailing list
Hi Roelof,

I could file-in or look at your attached code to try to help, 
but its not clear what your question is or how to reproduce your scenario.

You say... "when I call on the class side the method which should read the masses"    which is a non-specific **description**.
So I feel its extra work to try and decipher your question and perhaps I won't work it out and it would be wasted effort,
which discourages me from trying to answer.
Being more specific, something like... "when I call MyClass class>>myMethodXyz" 
would make feel like I have a chance at answering.

Same for... "at the process method" - I see no method named #process so I can't guess what you question is.

I've taken the time to provide this feedback to encourage you to improve your question to facilitate a better response.
I recommend reading... http://www.catb.org/~esr/faqs/smart-questions.html  (although the tone is a bit dark, there are some useful tidbits)

cheers -ben

P.S. Before your next (improved :^)  restatement of your question, could you try an exercise...
Slowly debug into the class-side call you are making, each step recording on paper on a new line the message sent, and on the same line the value of significant variables, to see if anything unexpected catches your attention.  

When the stepping is at a return statement ($^), select that statement and Inspect it, so you are sure of the value being returned. 

HTH.  
And btw, keep at it.  The "process" of finding out why things don't work as expected is where you learn the most.

On Tue, 31 Dec 2019 at 22:02, Roelof Wobben via Pharo-users <[hidden email]> wrote:
Hello,

Im still trying to make part1 of day2 working at a way I can also test
things.
the tests are working but when I call on the class side the method which
should reed the masses which are on the instanc side , the masses
cannnot be found.

So question 1 is why is masses not found.

and question 2  is how can I use the changed array at the process method
so I can calculate the outcome.

Regards,

Roelof

Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Esteban A. Maringolo
In reply to this post by Pharo Smalltalk Users mailing list
On Tue, Dec 31, 2019 at 11:29 AM Roelof Wobben via Pharo-users
<[hidden email]> wrote:

>
> I solved it but I think with ugly code on the class side.
>
> solution
>      | computer |
>      computer := self new.
>      computer readRam: computer masses.
>      computer patchRam: 1 value: 12.
>      computer patchRam: 2 value: 2.
>      computer processData: computer ram.
>      computer at: 0
>
> Is there a way I can make this more smalltalk.

I don't know exactly why you mean by "more smalltalk", I don't know
the problem you're solving, but you have a class side method that
doesn't return anything, and the readRam: and processData: methods
pass arguments that the receiver already have.

So I'd do:

solution
     | computer |
     computer := self new.
     computer readMasses;
     computer patchRam: 1 value: 12.
     computer patchRam: 2 value: 2.
     computer processRAMData.
     ^computer at: 0 "I don't know what this does".

Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Sean P. DeNigris
Administrator
In reply to this post by Pharo Smalltalk Users mailing list
Pharo Smalltalk Users mailing list wrote
> when I call on the class side the method which
> should reed the masses which are on the instanc side , the masses
> cannnot be found.

IntComputer class >> solution
        | computer ram |
        computer := self new.
        computer readRam: masses.
        ...

In your code above, you are not actually "calling a class-side method"
(which BTW in Smalltalk the term is "sending a message", an important
philosophical distinction if you want to grok Smalltalk), but referring to a
class-side instance variable named "masses". The clue to this would have
been that when you tried to compile the method, a dialog came up telling you
about the undeclared variable. You seem to have clicked through choosing to
create a variable, which you can now see in the class-side class definition:
    IntComputer class
        instanceVariableNames: 'masses'

Probably you forgot to add "computer" before "masses", like so: `computer
readRam: computer masses.`

For future exploration, there are some design smells in your code, but it
seems like you're currently at the "get it to work" stage, so it seems
premature to dive into them here.

NB: It will be much easier to help you in the future, if you include both of
the following in your questions:
1. the exact error
2. "steps to reproduce"

IOW in this case, something like: "I'm seeing '#splitOn: was sent to nil'
from IntComputer class>>#readRam:. To reproduce, do `IntComputer solution`"

This way we don't have to go hunting for an entry point, which will stop
many (busy) people from even trying!



-----
Cheers,
Sean
--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Pharo Smalltalk Users mailing list
Hello,

I have it now working but as you said there is a lot of code smells and
my exact question is how to get rid of them.

solution
      | computer |
      computer := self new.
      computer readRam: computer masses.
      computer patchRam: 1 value: 12.
      computer patchRam: 2 value: 2.
      computer processData: computer ram.
      computer at: 0


this is now a class method but I need a lot the computer variable.


here you can find the whole code under intComputer class.






Op 31-12-2019 om 19:29 schreef Sean P. DeNigris:

> Pharo Smalltalk Users mailing list wrote
>> when I call on the class side the method which
>> should reed the masses which are on the instanc side , the masses
>> cannnot be found.
> IntComputer class >> solution
> | computer ram |
> computer := self new.
> computer readRam: masses.
> ...
>
> In your code above, you are not actually "calling a class-side method"
> (which BTW in Smalltalk the term is "sending a message", an important
> philosophical distinction if you want to grok Smalltalk), but referring to a
> class-side instance variable named "masses". The clue to this would have
> been that when you tried to compile the method, a dialog came up telling you
> about the undeclared variable. You seem to have clicked through choosing to
> create a variable, which you can now see in the class-side class definition:
>      IntComputer class
> instanceVariableNames: 'masses'
>
> Probably you forgot to add "computer" before "masses", like so: `computer
> readRam: computer masses.`
>
> For future exploration, there are some design smells in your code, but it
> seems like you're currently at the "get it to work" stage, so it seems
> premature to dive into them here.
>
> NB: It will be much easier to help you in the future, if you include both of
> the following in your questions:
> 1. the exact error
> 2. "steps to reproduce"
>
> IOW in this case, something like: "I'm seeing '#splitOn: was sent to nil'
> from IntComputer class>>#readRam:. To reproduce, do `IntComputer solution`"
>
> This way we don't have to go hunting for an entry point, which will stop
> many (busy) people from even trying!
>
>
>
> -----
> Cheers,
> Sean
> --
> Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
>
>


Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Pharo Smalltalk Users mailing list
Op 1-1-2020 om 17:27 schreef Roelof Wobben via Pharo-users:


Hello,

I made some changes. Is this better or are there still some code smells.
Code : https://github.com/rwobben/intComputer/blob/master/AOC%202019/IntComputer.class.st

if so, can someone help me to solve the code smells.

Here is the challenge I try to solve :

On the way to your gravity assist around the Moon, your ship computer beeps angrily about a "1202 program alarm". On the radio, an Elf is already explaining how to handle the situation: "Don't worry, that's perfectly norma--" The ship computer bursts into flames.

You notify the Elves that the computer's magic smoke seems to have escaped. "That computer ran Intcode programs like the gravity assist program it was working on; surely there are enough spare parts up there to build a new Intcode computer!"

An Intcode program is a list of integers separated by commas (like 1,0,0,3,99). To run one, start by looking at the first integer (called position 0). Here, you will find an opcode - either 1, 2, or 99. The opcode indicates what to do; for example, 99 means that the program is finished and should immediately halt. Encountering an unknown opcode means something went wrong.

Opcode 1 adds together numbers read from two positions and stores the result in a third position. The three integers immediately after the opcode tell you these three positions - the first two indicate the positions from which you should read the input values, and the third indicates the position at which the output should be stored.

For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 10 and 20, add those values, and then overwrite the value at position 30 with their sum.

Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them. Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.

Once you're done processing an opcode, move to the next one by stepping forward 4 positions.

For example, suppose you have the following program:

1,9,10,3,2,3,11,0,99,30,40,50

For the purposes of illustration, here is the same program split into multiple lines:

1,9,10,3,
2,3,11,0,
99,
30,40,50

The first four integers, 1,9,10,3, are at positions 0, 1, 2, and 3. Together, they represent the first opcode (1, addition), the positions of the two inputs (9 and 10), and the position of the output (3). To handle this opcode, you first need to get the values at the input positions: position 9 contains 30, and position 10 contains 40. Add these numbers together to get 70. Then, store this value at the output position; here, the output position (3) is at position 3, so it overwrites itself. Afterward, the program looks like this:

1,9,10,70,
2,3,11,0,
99,
30,40,50

Step forward 4 positions to reach the next opcode, 2. This opcode works just like the previous, but it multiplies instead of adding. The inputs are at positions 3 and 11; these positions contain 70 and 50 respectively. Multiplying these produces 3500; this is stored at position 0:

3500,9,10,70,
2,3,11,0,
99,
30,40,50

Stepping forward 4 more positions arrives at opcode 99, halting the program.

Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Ben Coman
Hi Roelof,

I presume its working since you only asked for code smells.   
It looks pretty good to me with methods generally being small and doing one thing.    

The way your class-side-method "IntComputer class >> solution" just creates an instance and sends #process is good.
I'm a little curious that it doesn't return a value.  #process returns a value but nothing is done with it.  I guess the test-frame doesn't require it.

The way zero-index ram access is encapsulated by  #at:  and   #at:put:  is good.

Possible cleanup,  #in and #in: don't seem required since variable "in" is used directly in the rest of the code, which is fine.

The hardcoded  #masses  return value is fine, except it makes the "masses" class variable redundant.

Your various #processXXX methods are succinct and consume the stream neatly.

However its curious your  #processData and #processData:  methods have identical code, since the latter has an argument that is then not used.
its just lucky that the "self ram" you pass there to #processData:  is the same as the "ram" variable referenced 

Well done.
cheers -ben





On Thu, 2 Jan 2020 at 23:37, Roelof Wobben via Pharo-users <[hidden email]> wrote:
Op 1-1-2020 om 17:27 schreef Roelof Wobben via Pharo-users:
Hello,

I made some changes. Is this better or are there still some code smells.
Code : https://github.com/rwobben/intComputer/blob/master/AOC%202019/IntComputer.class.st

if so, can someone help me to solve the code smells.

Here is the challenge I try to solve :

On the way to your gravity assist around the Moon, your ship computer beeps angrily about a "1202 program alarm". On the radio, an Elf is already explaining how to handle the situation: "Don't worry, that's perfectly norma--" The ship computer bursts into flames.

You notify the Elves that the computer's magic smoke seems to have escaped. "That computer ran Intcode programs like the gravity assist program it was working on; surely there are enough spare parts up there to build a new Intcode computer!"

An Intcode program is a list of integers separated by commas (like 1,0,0,3,99). To run one, start by looking at the first integer (called position 0). Here, you will find an opcode - either 1, 2, or 99. The opcode indicates what to do; for example, 99 means that the program is finished and should immediately halt. Encountering an unknown opcode means something went wrong.

Opcode 1 adds together numbers read from two positions and stores the result in a third position. The three integers immediately after the opcode tell you these three positions - the first two indicate the positions from which you should read the input values, and the third indicates the position at which the output should be stored.

For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 10 and 20, add those values, and then overwrite the value at position 30 with their sum.

Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them. Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.

Once you're done processing an opcode, move to the next one by stepping forward 4 positions.

For example, suppose you have the following program:

1,9,10,3,2,3,11,0,99,30,40,50

For the purposes of illustration, here is the same program split into multiple lines:

1,9,10,3,
2,3,11,0,
99,
30,40,50

The first four integers, 1,9,10,3, are at positions 0, 1, 2, and 3. Together, they represent the first opcode (1, addition), the positions of the two inputs (9 and 10), and the position of the output (3). To handle this opcode, you first need to get the values at the input positions: position 9 contains 30, and position 10 contains 40. Add these numbers together to get 70. Then, store this value at the output position; here, the output position (3) is at position 3, so it overwrites itself. Afterward, the program looks like this:

1,9,10,70,
2,3,11,0,
99,
30,40,50

Step forward 4 positions to reach the next opcode, 2. This opcode works just like the previous, but it multiplies instead of adding. The inputs are at positions 3 and 11; these positions contain 70 and 50 respectively. Multiplying these produces 3500; this is stored at position 0:

3500,9,10,70,
2,3,11,0,
99,
30,40,50

Stepping forward 4 more positions arrives at opcode 99, halting the program.

Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Pharo Smalltalk Users mailing list
Hello Ben.

That was a error . There schould be a caret for it.  Im talking about the solution method.

No, I cannot clean the in part because it is used in the process*  methods

The  processData:  can also be deleted . I think Its  a part of a earlier try to solve it.

and yes. the ram is the same at both places. Is there then a better way to pass the stream around.

and of course thanks for the feedback.

Roelof



Op 2-1-2020 om 14:27 schreef Ben Coman:
Hi Roelof,

I presume its working since you only asked for code smells.   
It looks pretty good to me with methods generally being small and doing one thing.    

The way your class-side-method "IntComputer class >> solution" just creates an instance and sends #process is good.
I'm a little curious that it doesn't return a value.  #process returns a value but nothing is done with it.  I guess the test-frame doesn't require it.

The way zero-index ram access is encapsulated by  #at:  and   #at:put:  is good.

Possible cleanup,  #in and #in: don't seem required since variable "in" is used directly in the rest of the code, which is fine.

The hardcoded  #masses  return value is fine, except it makes the "masses" class variable redundant.

Your various #processXXX methods are succinct and consume the stream neatly.

However its curious your  #processData and #processData:  methods have identical code, since the latter has an argument that is then not used.
its just lucky that the "self ram" you pass there to #processData:  is the same as the "ram" variable referenced 

Well done.
cheers -ben





On Thu, 2 Jan 2020 at 23:37, Roelof Wobben via Pharo-users <[hidden email]> wrote:
Op 1-1-2020 om 17:27 schreef Roelof Wobben via Pharo-users:
Hello,

I made some changes. Is this better or are there still some code smells.
Code : https://github.com/rwobben/intComputer/blob/master/AOC%202019/IntComputer.class.st

if so, can someone help me to solve the code smells.

Here is the challenge I try to solve :

On the way to your gravity assist around the Moon, your ship computer beeps angrily about a "1202 program alarm". On the radio, an Elf is already explaining how to handle the situation: "Don't worry, that's perfectly norma--" The ship computer bursts into flames.

You notify the Elves that the computer's magic smoke seems to have escaped. "That computer ran Intcode programs like the gravity assist program it was working on; surely there are enough spare parts up there to build a new Intcode computer!"

An Intcode program is a list of integers separated by commas (like 1,0,0,3,99). To run one, start by looking at the first integer (called position 0). Here, you will find an opcode - either 1, 2, or 99. The opcode indicates what to do; for example, 99 means that the program is finished and should immediately halt. Encountering an unknown opcode means something went wrong.

Opcode 1 adds together numbers read from two positions and stores the result in a third position. The three integers immediately after the opcode tell you these three positions - the first two indicate the positions from which you should read the input values, and the third indicates the position at which the output should be stored.

For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 10 and 20, add those values, and then overwrite the value at position 30 with their sum.

Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them. Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.

Once you're done processing an opcode, move to the next one by stepping forward 4 positions.

For example, suppose you have the following program:

1,9,10,3,2,3,11,0,99,30,40,50

For the purposes of illustration, here is the same program split into multiple lines:

1,9,10,3,
2,3,11,0,
99,
30,40,50

The first four integers, 1,9,10,3, are at positions 0, 1, 2, and 3. Together, they represent the first opcode (1, addition), the positions of the two inputs (9 and 10), and the position of the output (3). To handle this opcode, you first need to get the values at the input positions: position 9 contains 30, and position 10 contains 40. Add these numbers together to get 70. Then, store this value at the output position; here, the output position (3) is at position 3, so it overwrites itself. Afterward, the program looks like this:

1,9,10,70,
2,3,11,0,
99,
30,40,50

Step forward 4 positions to reach the next opcode, 2. This opcode works just like the previous, but it multiplies instead of adding. The inputs are at positions 3 and 11; these positions contain 70 and 50 respectively. Multiplying these produces 3500; this is stored at position 0:

3500,9,10,70,
2,3,11,0,
99,
30,40,50

Stepping forward 4 more positions arrives at opcode 99, halting the program.


Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Sean P. DeNigris
Administrator
In reply to this post by Pharo Smalltalk Users mailing list
Pharo Smalltalk Users mailing list wrote
>    I made some changes. Is this better or are there still some code
>     smells.

I don't have time for an in-depth review, but I skimmed it and it seems
better.



-----
Cheers,
Sean
--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: why is masses not found?

Ben Coman
In reply to this post by Ben Coman


On Fri, 3 Jan 2020 at 00:41, Roelof Wobben <[hidden email]> wrote:
Hello Ben.

That was a error . There schould be a caret for it.  Im talking about the solution method.

No, I cannot clean the in part because it is used in the process*  methods

But its not.  Your statement would be true if your processXXX methods did...
    a := self at: self in next.
but they don't, instead you have...
    a := self at: in next.

You never send the #in message, which is fine since "information hiding" can be useful 
to avoid exposing your internal representation that the outside doesn't need to know about.
(the proof is in the pudding, see if it works after deleting #in and #in:)

The  processData:  can also be deleted . I think Its  a part of a earlier try to solve it.

and yes. the ram is the same at both places.
 
Is there then a better way to pass the stream around.

I think its fine how you've done it.

Final thing is to clean up the "class-instance variable" (i.e. class-side instance variable)
which Sean mentioned.  You can see it here...

To remove it from the image, in the System Browser while viewing the class definition, click the "Class" button and you should see it.

cheers -ben  



Op 2-1-2020 om 14:27 schreef Ben Coman:
Hi Roelof,

I presume its working since you only asked for code smells.   
It looks pretty good to me with methods generally being small and doing one thing.    

The way your class-side-method "IntComputer class >> solution" just creates an instance and sends #process is good.
I'm a little curious that it doesn't return a value.  #process returns a value but nothing is done with it.  I guess the test-frame doesn't require it.

The way zero-index ram access is encapsulated by  #at:  and   #at:put:  is good.

Possible cleanup,  #in and #in: don't seem required since variable "in" is used directly in the rest of the code, which is fine.

The hardcoded  #masses  return value is fine, except it makes the "masses" class variable redundant.

Your various #processXXX methods are succinct and consume the stream neatly.

However its curious your  #processData and #processData:  methods have identical code, since the latter has an argument that is then not used.
its just lucky that the "self ram" you pass there to #processData:  is the same as the "ram" variable referenced 

Well done.
cheers -ben





On Thu, 2 Jan 2020 at 23:37, Roelof Wobben via Pharo-users <[hidden email]> wrote:
Op 1-1-2020 om 17:27 schreef Roelof Wobben via Pharo-users:
Hello,

I made some changes. Is this better or are there still some code smells.
Code : https://github.com/rwobben/intComputer/blob/master/AOC%202019/IntComputer.class.st

if so, can someone help me to solve the code smells.

Here is the challenge I try to solve :

On the way to your gravity assist around the Moon, your ship computer beeps angrily about a "1202 program alarm". On the radio, an Elf is already explaining how to handle the situation: "Don't worry, that's perfectly norma--" The ship computer bursts into flames.

You notify the Elves that the computer's magic smoke seems to have escaped. "That computer ran Intcode programs like the gravity assist program it was working on; surely there are enough spare parts up there to build a new Intcode computer!"

An Intcode program is a list of integers separated by commas (like 1,0,0,3,99). To run one, start by looking at the first integer (called position 0). Here, you will find an opcode - either 1, 2, or 99. The opcode indicates what to do; for example, 99 means that the program is finished and should immediately halt. Encountering an unknown opcode means something went wrong.

Opcode 1 adds together numbers read from two positions and stores the result in a third position. The three integers immediately after the opcode tell you these three positions - the first two indicate the positions from which you should read the input values, and the third indicates the position at which the output should be stored.

For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 10 and 20, add those values, and then overwrite the value at position 30 with their sum.

Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them. Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.

Once you're done processing an opcode, move to the next one by stepping forward 4 positions.

For example, suppose you have the following program:

1,9,10,3,2,3,11,0,99,30,40,50

For the purposes of illustration, here is the same program split into multiple lines:

1,9,10,3,
2,3,11,0,
99,
30,40,50

The first four integers, 1,9,10,3, are at positions 0, 1, 2, and 3. Together, they represent the first opcode (1, addition), the positions of the two inputs (9 and 10), and the position of the output (3). To handle this opcode, you first need to get the values at the input positions: position 9 contains 30, and position 10 contains 40. Add these numbers together to get 70. Then, store this value at the output position; here, the output position (3) is at position 3, so it overwrites itself. Afterward, the program looks like this:

1,9,10,70,
2,3,11,0,
99,
30,40,50

Step forward 4 positions to reach the next opcode, 2. This opcode works just like the previous, but it multiplies instead of adding. The inputs are at positions 3 and 11; these positions contain 70 and 50 respectively. Multiplying these produces 3500; this is stored at position 0:

3500,9,10,70,
2,3,11,0,
99,
30,40,50

Stepping forward 4 more positions arrives at opcode 99, halting the program.