-
Notifications
You must be signed in to change notification settings - Fork 1
/
advent.lua
executable file
·136 lines (111 loc) · 3.8 KB
/
advent.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/local/bin/lumarsh
---@diagnostic disable: lowercase-global
sh = sh
LUMARSH_ARGS = LUMARSH_ARGS
langs = {}
langs.asm = {}
langs.c = {}
langs.d = {}
langs.go = {}
langs.python = {}
langs.csharp = {}
langs.fsharp = {}
-- Helpers
function genericScaffold(folder, language)
folder = sh.path.buildPath({folder, language})
if not sh.fs.exists(folder) then
sh.fs.mkdirRecurse(folder)
sh:cp("-r", "templates/"..language, sh.path.buildPath({folder, ".."}))
end
end
-- NOTE: The .Net languages only have Millisecond precision
function timeSolution(exe, args)
local output = sh.proc.executeEnforceZero(exe, args or {}).output
local match = sh.regex.matchFirst(output, "__TIME__ = (\\d+)")
assert(match.matched, "Regex failed?")
return tonumber(match.captures[2]) -- Microseconds
end
function mesonRun(folder, language)
folder = sh.path.buildPath({folder, language})
local cdir = sh.path.getcwd()
sh.fs.chdir(folder)
sh.proc.executeShell('meson', {'build'})
sh.fs.chdir('build')
sh:ninja()
local result = timeSolution("./solution")
sh.fs.chdir(cdir)
return result
end
function makeRun(folder, language)
folder = sh.path.buildPath({folder, language})
local cdir = sh.path.getcwd()
sh.fs.chdir(folder)
sh:make('-B')
local result = timeSolution("./solution")
sh.fs.chdir(cdir)
return result
end
function genericRun(folder, lang, exe, args)
folder = sh.path.buildPath({folder, lang})
local cdir = sh.path.getcwd()
sh.fs.chdir(folder)
local result = timeSolution(exe, args)
sh.fs.chdir(cdir)
return result
end
-- Languages
function langs.asm.run(folder) return makeRun(folder, 'asm') end
function langs.asm.scaffold(folder) genericScaffold(folder, 'asm') end
function langs.c.run(folder) return mesonRun(folder, 'c') end
function langs.c.scaffold(folder) genericScaffold(folder, 'c') end
function langs.d.run(folder) return mesonRun(folder, 'd') end
function langs.d.scaffold(folder) genericScaffold(folder, 'd') end
function langs.go.run(folder) return genericRun(folder, 'go', 'go', {'run', '.'}) end
function langs.go.scaffold(folder) genericScaffold(folder, 'go') end
function langs.python.run(folder) return genericRun(folder, 'python', 'python', {'./app.py'}) end
function langs.python.scaffold(folder) genericScaffold(folder, 'python') end
function langs.csharp.run(folder) return genericRun(folder, 'csharp', 'dotnet', {'run'}) end
function langs.csharp.scaffold(folder) genericScaffold(folder, 'csharp') end
function langs.fsharp.run(folder) return genericRun(folder, 'fsharp', 'dotnet', {'run'}) end
function langs.fsharp.scaffold(folder) genericScaffold(folder, 'fsharp') end
-- Commands
if LUMARSH_ARGS and LUMARSH_ARGS[1] == "test" then
if sh.fs.exists("test") then
sh:rm("-rf", "test")
end
for lang,v in pairs(langs) do
if v.scaffold then
v.scaffold("test")
v.run("test")
end
end
elseif LUMARSH_ARGS and LUMARSH_ARGS[1] == "scaffold" then
local day = LUMARSH_ARGS[2]
assert(day, "Please provide which day to scaffold")
if not sh.fs.exists(day) then
sh.fs.mkdirRecurse(day)
end
if not sh.fs.exists(day.."/input.txt") then
sh:cp("templates/input.txt", day)
end
for lang,v in pairs(langs) do
if v.scaffold then
v.scaffold(day)
v.run(day) -- Ensure it compiles
end
end
elseif LUMARSH_ARGS and LUMARSH_ARGS[1] == "run" then
local day = LUMARSH_ARGS[2]
assert(day, "Please provide which day to scaffold")
local l = LUMARSH_ARGS[3]
for lang,v in pairs(langs) do
if not l or lang == l then
v.result = v.run(day)
end
end
for lang,v in pairs(langs) do
if v.result then
print(lang, v.result)
end
end
end