我选择尝试适用于AOC 2022益智4的Smalltalk。如果满足约束,我将对每一行进行预测并增加计数器。我想弄明白为什么'2-8,3-7‘线不符合要求。因此,我开始打印值来检查正在发生的事情。显然,当通过向对象发送displayNl消息输出值时,firstMax、firstMin等值通过循环总是相同的,其中包含来自‘2-4、6-8’的信息,即第一行。但是,更奇怪的是,即使第一行不满足约束,计数器也会增加一次。然后,我发现在检查'6-6,4-6‘行时,它实际上计算了布尔值overlapFirst和overlapSecond值,因此ifTrue会增加计数器!为什么!?
编辑:我解决了这个问题,把它放在这里,而不是首先将子字符串放入一个变量中:
firstAssignment := (line substrings: ',') first.
secondAssignment := (line substrings: ',') last. 这是否意味着您不能重新分配OrderedCollection?
通过运行命令:gst main.st,我正在用gnu小对话来运行这个程序。
这是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这是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.发布于 2022-12-04 08:40:58
这解决了我的问题..。我还编辑了这篇文章,我需要学习如何在堆栈溢出中做一些事情。
我换了第5行和第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.https://stackoverflow.com/questions/74673633
复制相似问题