Petr Schreiber
09-12-2024, 12:10
Hi,
there is a nice algorithmic challenge each year, called Advent of Code.
Link for this year is:
https://adventofcode.com/2024
I attach my naive solutions, we can think if and how they can be improved, if you want.
SPOILERS BELOW
Link to the task assignment: https://adventofcode.com/2024/day/1
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
' First, let's load each column individually
long leftList(entryCount)
long rightList(entryCount)
long columns(2)
for i as long = 1 to entryCount
split(lines(i), " ", columns)
leftList(i) = columns(1)
rightList(i) = columns(2)
next
' ...for easy individual sort
array sort leftList(), ascend
array sort rightList(), ascend
' ...to calculate distance score
long distance
for i as long = 1 to entryCount
distance += abs(rightList(i) - leftList(i))
next
printl distance, "is the distance" in 14
waitkey
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
' In theory, I would not need to load whole file to memory in this exercise, but it is small
string inputContent = file_load(inputFile)
string lines()
long entryCount = parse(inputContent, lines, $LF) - 1 ' Last line left empty
' I will store key-value pairs of number & its occurence count for each column
dword totalColumnNumberCounts(2) = hash_new(entryCount), hash_new(entryCount)
long rowValue(2)
' Let's find number of value occurences in each column
long idOccurrence(2)
for i as long = 1 to entryCount
split(lines(i), " ", rowValue)
IncreaseHashValue(totalColumnNumberCounts(1), rowValue(1), 1)
IncreaseHashValue(totalColumnNumberCounts(2), rowValue(2), 1)
next
' First, we need to find unique keys in the left column
string idsCommaSeparated = Hash_GetKeys(totalColumnNumberCounts(1), ",")
string id()
parse(idsCommaSeparated, id, ",")
long similarity
' Second, we calculate the similarity score
for i as long = 1 to countof(id)
idOccurrence(1) = hash_get(totalColumnNumberCounts(1), id(i))
idOccurrence(2) = hash_get(totalColumnNumberCounts(2), id(i))
similarity += val(id(i)) * idOccurrence(1) * idOccurrence(2)
next
hash_free(totalColumnNumberCounts(1))
hash_free(totalColumnNumberCounts(2))
printl similarity, "is the similarity score" in 14
waitkey
end function
function IncreaseHashValue(hashHandle as dword, hashValue as long, amount as long)
long currentCount = val(hash_get(hashHandle, hashValue)) + amount
hash_set(hashHandle, hashValue, currentCount)
end function
there is a nice algorithmic challenge each year, called Advent of Code.
Link for this year is:
https://adventofcode.com/2024
I attach my naive solutions, we can think if and how they can be improved, if you want.
SPOILERS BELOW
Link to the task assignment: https://adventofcode.com/2024/day/1
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
' First, let's load each column individually
long leftList(entryCount)
long rightList(entryCount)
long columns(2)
for i as long = 1 to entryCount
split(lines(i), " ", columns)
leftList(i) = columns(1)
rightList(i) = columns(2)
next
' ...for easy individual sort
array sort leftList(), ascend
array sort rightList(), ascend
' ...to calculate distance score
long distance
for i as long = 1 to entryCount
distance += abs(rightList(i) - leftList(i))
next
printl distance, "is the distance" in 14
waitkey
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
' In theory, I would not need to load whole file to memory in this exercise, but it is small
string inputContent = file_load(inputFile)
string lines()
long entryCount = parse(inputContent, lines, $LF) - 1 ' Last line left empty
' I will store key-value pairs of number & its occurence count for each column
dword totalColumnNumberCounts(2) = hash_new(entryCount), hash_new(entryCount)
long rowValue(2)
' Let's find number of value occurences in each column
long idOccurrence(2)
for i as long = 1 to entryCount
split(lines(i), " ", rowValue)
IncreaseHashValue(totalColumnNumberCounts(1), rowValue(1), 1)
IncreaseHashValue(totalColumnNumberCounts(2), rowValue(2), 1)
next
' First, we need to find unique keys in the left column
string idsCommaSeparated = Hash_GetKeys(totalColumnNumberCounts(1), ",")
string id()
parse(idsCommaSeparated, id, ",")
long similarity
' Second, we calculate the similarity score
for i as long = 1 to countof(id)
idOccurrence(1) = hash_get(totalColumnNumberCounts(1), id(i))
idOccurrence(2) = hash_get(totalColumnNumberCounts(2), id(i))
similarity += val(id(i)) * idOccurrence(1) * idOccurrence(2)
next
hash_free(totalColumnNumberCounts(1))
hash_free(totalColumnNumberCounts(2))
printl similarity, "is the similarity score" in 14
waitkey
end function
function IncreaseHashValue(hashHandle as dword, hashValue as long, amount as long)
long currentCount = val(hash_get(hashHandle, hashValue)) + amount
hash_set(hashHandle, hashValue, currentCount)
end function