PDA

View Full Version : Advent of Code, 2024 - Day 03



Petr Schreiber
23-12-2024, 14:11
Hi,

I would like to share a possible solution for day 3 of Advent of Code 2024.

SPOILERS BELOW

The assignment can be found at https://adventofcode.com/2024/day/3
Please note that to reach the assignment you need to complete the assignment 2 (https://www.thinbasic.com/community/showthread.php?13372-Advent-of-Code-2024-Day-02) 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

This is the first assignment when correct pattern matching is required, in this case extracting valid mul(number1, number2) sequences again with some limitations. Regex can be used in such cases nicely.


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)

long result = CalculateValidMuls(inputContent)

printl
printl result, "is the result" in 14

waitkey
end function

function CalculateValidMuls(inputText as string) as long
long startPos = 1
long lastLen = 0
long result
string textFound

while startPos > 0
textFound = regexpr$("mul[(][0-9]+[,][0-9]+[)]", inputText, startPos, startPos, lastLen)

startPos = startPos + lastLen

if startPos > 0 then
result += val(grab$(textFound, "(", ",")) * val(grab$(textFound, ",", ")"))
printl textFound
end if
wend

return result
end function


Part 2

Second part complicates the assignment by introduction of do() and don't() directives, which serve as flags enabling and disabling the validity of mul statements.


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)

long result = CalculateValidMulsFromEnabledSections(inputContent)

printl
printl result, "is the result" in 14

waitkey
end function

function CalculateValidMulsFromEnabledSections(inputText as string) as long
inputText = "do()" + inputText
long result

long pos = 1
long nextDo, nextDont

Repeat
nextDo = instr(pos, inputText, "do()")
nextDont = instr(nextDo, inputText, "don't()")

result += CalculateValidMuls(mid$(inputText, nextDo, iif(nextDont = 0, len(inputText), nextDont)-nextDo))

pos = nextDont
until pos = 0

return result
end function

function CalculateValidMuls(inputText as string) as long
long startPos = 1
long lastLen = 0
long result
string textFound

while startPos > 0
textFound = regexpr$("mul[(][0-9]+[,][0-9]+[)]", inputText, startPos, startPos, lastLen)

startPos = startPos + lastLen

if startPos > 0 then
result += val(grab$(textFound, "(", ",")) * val(grab$(textFound, ",", ")"))
printl textFound
end if
wend

return result
end function

Petr