From 0ea10a3c1b1ccf83a243bad6e72739f90636bb31 Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Mon, 2 Sep 2024 15:15:28 +0200 Subject: [PATCH] Ultra slow lua solution (aka as slow as my naive rust solution) --- src/main/lua/main.lua | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/main/lua/main.lua diff --git a/src/main/lua/main.lua b/src/main/lua/main.lua new file mode 100644 index 0000000..a13694a --- /dev/null +++ b/src/main/lua/main.lua @@ -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("}")