Hi,
I would like to share a possible solution for day 2 of Advent of Code 2024.
SPOILERS BELOW
The assignment can be found at https://adventofcode.com/2024/day/2
Please note that to reach the assignment you need to complete the assignment 1 first.
Please make sure you download your input.txt file from the page above and that you pass it as parameter to the scripts below (for example in thinAir, you enter input.txt to Script/Command line... if you stored the file side by side with the solutions)
Part 1
uses "console", "file"
function tbmain()
string inputFile = app.ArgV(2)
if not file_exists(inputFile) then
printl inputFile
printl "Please specify valid input file as first parameter of the script"
waitkey
return 1
end if
string inputContent = file_load(inputFile)
string lines()
long entryCount = parse(inputContent, lines, $LF) - 1 ' Last line left empty
long i
long sequenceRating, safeCount
string description
for i = 1 to entryCount
' Printing the analysed line + extra space to space out later rating
print lset$(lines(i), 32 using " ")
sequenceRating = GetSequenceRating(lines(i), description)
if sequenceRating = 0 Then
incr safeCount
end if
' Optional explanation of sequence rating
printl description in iif(sequenceRating = 0, 10, 12)
next
printl
printl safeCount, "safe items" in 14
waitkey
end function
function GetSequenceRating(sequenceText as string, byref description as string) as long
long j, originalDirection, direction, diff
long values()
long valueCount = split(sequenceText, " ", values)
for j = 2 to valueCount
diff = values(j) - values(j-1)
direction = sgn(diff)
if j = 2 then
if diff = 0 Then
description = "UNSAFE - no direction"
return 1
end if
originalDirection = direction
end if
if direction <> originalDirection Then
description = "UNSAFE - inconsistent direction"
return 2
end if
if not between(abs(diff), 1, 3) Then
description = "UNSAFE - too big gap"
return 3
end if
if j = valueCount then
description = "SAFE"
return 0
end if
next
end function
Part 2
uses "console", "file"
function tbmain()
string inputFile = app.ArgV(2)
if not file_exists(inputFile) then
printl inputFile
printl "Please specify valid input file as first parameter of the script"
waitkey
return 1
end if
string inputContent = file_load(inputFile)
string lines()
long entryCount = parse(inputContent, lines, $LF) - 1 ' Last line left empty
long i, j
long sequenceRating, safeCount
string description, alteredSequence
for i = 1 to entryCount
' Printing the analysed line + extra space to space out later rating
print lset$(lines(i), 32 using " ")
sequenceRating = GetSequenceRating(lines(i), description)
if sequenceRating = 0 Then
incr safeCount
else
' The original form is not valid, let's alter it by removing specific element
for j = 1 to parsecount(lines(i), " ")
alteredSequence = GetSequenceWithoutNthElement(lines(i), j)
if GetSequenceRating(alteredSequence, description) = 0 Then
incr safeCount
exit for
end if
next
end if
' Optional explanation of sequence rating
printl description in iif(sequenceRating = 0, 10, 12)
next
printl
printl safeCount, "safe items" in 14
waitkey
end function
function GetSequenceRating(sequenceText as string, byref description as string) as long
long j, originalDirection, direction, diff
long values()
long valueCount = split(sequenceText, " ", values)
for j = 2 to valueCount
diff = values(j) - values(j-1)
direction = sgn(diff)
if j = 2 then
if diff = 0 Then
description = "UNSAFE - no direction"
return 1
end if
originalDirection = direction
end if
if direction <> originalDirection Then
description = "UNSAFE - inconsistent direction"
return 2
end if
if not between(abs(diff), 1, 3) Then
description = "UNSAFE - too big gap"
return 3
end if
if j = valueCount then
description = "SAFE"
return 0
end if
next
end function
function GetSequenceWithoutNthElement(sequenceText as string, n as long) as string
return trimfull$(ParseSet$(sequenceText, " ", n, ""))
end function
Petr
Bookmarks