2

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.
  • The quantities you compute `firstMin`, `firstMax` etc are `Strings` and the logic of your code expects them to be `Numbers`. To change this send `asInteger` to the RHS of the assignments. – Leandro Caniglia Dec 04 '22 at 08:22
  • @LeandroCaniglia Yes, I tried that myself, but it apparently it worked the same. However, I come up with a weird solution... See my edited post, do you have any idea why I couldn't reassign OrderedCollection? – Edvin Sinkevič Dec 04 '22 at 08:38
  • I'll be frank I don't know much about Smalltalk. But I would assume assignments is a local variable for that lambda :line -> ... . – Edvin Sinkevič Dec 04 '22 at 08:44
  • Not in this case. You have to explicitly declare local variables inside the block if you want them to be local to the block. – Leandro Caniglia Dec 04 '22 at 08:47

1 Answers1

1

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.