Day 2: Add Lua solution
This commit is contained in:
parent
4eed3518d6
commit
1ce45e6d9f
2 changed files with 31 additions and 0 deletions
15
02/solution_1.lua
Executable file
15
02/solution_1.lua
Executable file
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/env lua
|
||||
|
||||
h = 0
|
||||
v = 0
|
||||
|
||||
function down(val) v = v + val end
|
||||
function up(val) v = v - val end
|
||||
function forward(val) h = h + val end
|
||||
|
||||
for line in io.lines("input.txt") do
|
||||
cmd, val = line:gmatch("(%S+) (%S+)")()
|
||||
_G[cmd](val)
|
||||
end
|
||||
|
||||
print(("Answer: %d"):format(h * v))
|
16
02/solution_2.lua
Executable file
16
02/solution_2.lua
Executable file
|
@ -0,0 +1,16 @@
|
|||
#!/usr/bin/env lua
|
||||
|
||||
aim = 0
|
||||
h = 0
|
||||
v = 0
|
||||
|
||||
function down(val) aim = aim + val end
|
||||
function up(val) aim = aim - val end
|
||||
function forward(val) h = h + val; v = v + aim * val end
|
||||
|
||||
for line in io.lines("input.txt") do
|
||||
cmd, val = line:gmatch("(%S+) (%S+)")()
|
||||
_G[cmd](val)
|
||||
end
|
||||
|
||||
print(("Answer: %d"):format(h * v))
|
Loading…
Reference in a new issue