2
1
Fork 0

Day 2: Add Lua solution

This commit is contained in:
Mia Herkt 2021-12-02 12:43:26 +01:00
parent 4eed3518d6
commit 1ce45e6d9f
Signed by: mia
GPG key ID: 72E154B8622EC191
2 changed files with 31 additions and 0 deletions

15
02/solution_1.lua Executable file
View 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
View 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))