Ultra slow lua solution (aka as slow as my naive rust solution)

This commit is contained in:
Fabian Schmidt 2024-09-02 15:15:28 +02:00
parent 3dbc9c32d1
commit 0ea10a3c1b

57
src/main/lua/main.lua Normal file
View File

@ -0,0 +1,57 @@
local file_path = "../../../measurements.txt"
local file = io.open(file_path, "rb")
if not file then
print("Unable to open file")
return
end
local function split_semi(inputstr)
local t = {}
for str in string.gmatch(inputstr, "([^;]+)") do
table.insert(t, str)
end
return t
end
local stations = {}
local iterations = 1
for line in file:lines("*l") do
if iterations % 1000000 == 0 then
io.write("\x1b[J\x1b[H")
io.write(iterations / 10000000)
io.write("\n")
end
local split_line = split_semi(line)
local station = split_line[1]
local temp_str = string.gsub(split_line[2], "[ ]", "")
local temp = tonumber(temp_str)
if stations[station] == nil then
stations[station] = { min = temp, max = temp, sum = temp, count = 1 }
else
if temp < stations[station].min then
stations[station].min = temp
elseif temp > stations[station].max then
stations[station].max = temp
end
stations[station].sum = stations[station].sum + temp
stations[station].count = stations[station].count + 1
end
iterations = iterations + 1
end
local keys = {}
for k in pairs(stations) do table.insert(keys, k) end
table.sort(keys)
local fstations = {}
io.write("{")
for _, station in ipairs(keys) do
local avg = ((stations[station].sum / 10) / stations[station].count)
local res_str = string.format("%s=%.1f/%.1f/%.1f", station, stations[station].min, avg, stations[station].max)
table.insert(fstations, res_str)
end
io.write(table.concat(fstations, ","))
print("}")