adventofcode2022/day4/advent_of_code_day4_part2.txt

56 lines
1.2 KiB
Text
Raw Normal View History

2022-12-04 01:05:51 -05:00
@name Advent of Code Part 2
@inputs
@outputs
@persist Input:array [I Overlaps]:number
@trigger
runOnFile(1)
interval(100)
if(first()) {
fileLoad("input.txt")
}
if(fileClk()) {
Input = fileRead():explode("\n")
Input:removeString(Input:count())
I = 1
Overlaps = 0
}
if(fileLoaded() && I <= Input:count()) {
#printTable(Input)
while(perf()) {
#print(Input[I,string])
Thing = Input[I,string]:explode(",")
Range1 = Thing[1,string]:explode("-")
Range2 = Thing[2,string]:explode("-")
if(Range2[1,string]:toNumber() <= Range1[2,string]:toNumber()
&& Range2[2,string]:toNumber() >= Range1[1,string]:toNumber()) {
#print(I+": True1")
Overlaps++
} elseif (Range1[1,string]:toNumber() <= Range2[2,string]:toNumber()
&& Range1[2,string]:toNumber() >= Range2[1,string]:toNumber()) {
#print(I+": True2")
Overlaps++
} else {
#print(I+": False")
}
I++
if(I > Input:count()) {
break
}
}
}
if(fileLoaded() && I > Input:count()) {
print(Overlaps)
print(I)
runOnTick(0)
stoptimer("interval")
}