add utils, use luajit which seems faster

This commit is contained in:
Felix Van der Jeugt 2021-12-05 12:10:59 +01:00
parent 87637a4682
commit fad5da2e27
No known key found for this signature in database
GPG Key ID: 58B209295023754D
5 changed files with 33 additions and 25 deletions

View File

@ -1,15 +1,4 @@
function split(text, sep)
local parts = {}
local start = 0
local from, to = text:find(sep)
while from ~= nil do
table.insert(parts, text:sub(start, from - 1))
text = text:sub(to + 1)
from, to = text:find(sep)
end
table.insert(parts, text)
return parts
end
require("utils")
local f = io.open(arg[3])
local numbers = split(f:read("*l"), ",")

View File

@ -1,3 +1,5 @@
require("utils")
function readline()
local x1 = io.read("*n")
io.read(1) -- ","
@ -16,24 +18,19 @@ function mark(field, x, y)
field[x] = row
end
function sign(n)
if n == 0 then return 0
elseif n < 0 then return -1
else return 1
end
end
local input = io.open(arg[3]):read("*a")
local lines = split(input, "\n")
io.input(arg[3])
local x1, y1, x2, y2 = readline()
local field = {}
while x1 ~= nil do
for _, line in pairs(lines) do
local x1, y1, x2, y2 = unpack(split(line, "[^0-9]+"))
x1, y1, x2, y2 = tonumber(x1), tonumber(y1), tonumber(x2), tonumber(y2)
local x, y, dx, dy = x1, y1, sign(x2 - x1), sign(y2 - y1)
mark(field, x, y)
while x ~= x2 or y ~= y2 do
x, y = x + dx, y + dy
mark(field, x, y)
end
x1, y1, x2, y2 = readline()
end
local count = 0

View File

@ -5,9 +5,7 @@ part="$2"
day="$(printf '%02d' "$day")"
if test -f "day${day}/part${part}.lua"; then
luac -o luac.out "day${day}/part${part}.lua"
printf '#!/bin/sh\nlua luac.out "$@"\n' > run
chmod u+x run
luajit -v >/dev/null
elif test -f "day${day}/part${part}.hs"; then
#stack build
stack ghc -- -O3 "day${day}/part${part}.hs" -o run

5
run Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh
day="$1"
part="$2"
luajit "day${day}/part${part}.lua" "$@"

19
utils.lua Normal file
View File

@ -0,0 +1,19 @@
function split(text, sep)
local parts = {}
local start = 0
local from, to = text:find(sep)
while from ~= nil do
table.insert(parts, text:sub(start, from - 1))
text = text:sub(to + 1)
from, to = text:find(sep)
end
table.insert(parts, text)
return parts
end
function sign(n)
if n == 0 then return 0
elseif n < 0 then return -1
else return 1
end
end