smalltalkgnu-smalltalk

Smalltalk anomaly - why are the variables always the same, but when computing booleans they are different?


I chose to try out Smalltalk for AOC 2022 puzzle 4. I'm predicating on each line and increment the counter if the constraints are met. I'm trying to understand why the '2-8,3-7' line doesn't met the requirements. Therefore, I started printing out the values to check what's happening. Apparently, when printing out the values by sending displayNl message to the objects, the values firstMax, firstMin etc. are always the same through the loop, containing the info from '2-4,6-8', i.e. the first line. But still, what's even more weird, that the counter gets incremented once, even though the first line doesn't meet the constraints. Then, I figured out that it actually computes the boolean overlapFirst and overlapSecond values correctly, when checking the '6-6,4-6' line, hence ifTrue increments the counter! WHY!?

EDIT: I solved it by putting this instead of first putting the substrings into a variable:

firstAssignment := (line substrings: ',') first. 
secondAssignment := (line substrings: ',') last. 

Does it mean that you cannot reassign OrderedCollection?

I'm running this with gnu-small talk, by running command: gst main.st

Here's data.txt.


2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

Here's main.st.


file := FileStream open: 'data.txt' mode: FileStream read.
count := 0.
file linesDo: [ 
  :line | 
  assignments := line substrings: ','.
  firstAssignment := assignments first.
  secondAssignment := assignments last.
  first := firstAssignment substrings: '-'.
  second := secondAssignment substrings: '-'.
  firstMin := first first.
  firstMax := first last.
  secondMin := second first.
  secondMax := second last.
  overlapFirst := (firstMin <= secondMin) & (firstMax >= secondMax).
  overlapSecond := (secondMin <= firstMin) & (secondMax >= firstMax).

  overlap := overlapSecond | overlapFirst.

  line displayNl.
  overlapFirst displayNl.
  overlapSecond displayNl.
  firstMin displayNl.
  firstMax displayNl.
  secondMin displayNl.
  secondMax displayNl.

  overlap ifTrue: [
  'Incremented!' displayNl.
  count := count + 1.
  ].
].

Transcript show: count asString.


file close.

Solution

  • This solved my issue... I also edited the post, I'll need to learn how to do things in stackoverflow.

    I changed lines 5 and 6.

    file := FileStream open: 'data.txt' mode: FileStream read.
    count := 0.
    file linesDo: [ 
      :line | 
      firstAssignment := (line substrings: ',') first. 
      secondAssignment := (line substrings: ',') last.
      first := firstAssignment substrings: '-'.
      second := secondAssignment substrings: '-'.
      firstMin := first first asInteger.
      firstMax := first last asInteger.
      secondMin := second first asInteger.
      secondMax := second last asInteger.
      overlapFirst := (firstMin <= secondMin) & (firstMax >= secondMax).
      overlapSecond := (secondMin <= firstMin) & (secondMax >= firstMax).
    
      overlap := overlapSecond | overlapFirst.
    
      line displayNl.
    
      overlap ifTrue: [
      'Incremented!' displayNl.
      count := count + 1.
      ].
    ].
    
    Transcript show: count asString.
    
    
    file close.