Advent of Code 2019 - Lua
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

75 lines
1.7 KiB

signal = {}
file = io.open(arg[1])
for num in file:read("a"):gmatch("%d") do
table.insert(signal, tonumber(num))
end
file:close()
function calc(signal, num)
local fin = #signal
local cur = num
local val = 0
while cur <= fin do
for _, x in ipairs({1, 0, -1, 0}) do
if x == 0 then
cur = cur + num
else
local cur_val = 0
for i = 1, num do
cur_val = cur_val + signal[cur]
cur = cur + 1
if cur > fin then
break
end
end
val = val + cur_val * x
end
if cur > fin then
break
end
end
end
return math.abs(val) % 10
end
function calc_dumb_p2(signal, offset)
local val = 0
for i = #signal, offset, -1 do
val = (val + signal[i]) % 10
signal[i] = val
end
end
function signal_tostring(signal, start, len)
local str = ""
for i = start, len do
str = str .. string.format("%d", signal[i])
end
return str
end
offset = signal[1] * (10 ^ 6) + signal[2] * (10 ^ 5) + signal[3] * (10 ^ 4) + signal[4] * (10 ^ 3) + signal[5] * (10 ^ 2) + signal[6] * (10 ^ 1) + signal[7]
signal_p2 = {}
for i = 1, 10000 do
for i = 1, #signal do
table.insert(signal_p2, signal[i])
end
end
print(offset)
offset = offset % #signal_p2
print(offset)
for i = 1, 100 do
for i = 1, #signal do
signal[i] = calc(signal, i)
end
end
print("Part 1:", signal_tostring(signal, 1, 8))
for i = 1, 100 do
calc_dumb_p2(signal_p2, offset)
end
print("Part 2:", signal_tostring(signal_p2, offset + 1, offset + 8))