Browse Source

Add day 16

master
mid-kid 5 years ago
parent
commit
f964f370ee
  1. 75
      d16.lua
  2. 1
      d16_input.txt

75
d16.lua

@ -0,0 +1,75 @@
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))

1
d16_input.txt

@ -0,0 +1 @@
59717513948900379305109702352254961099291386881456676203556183151524797037683068791860532352118123252250974130706958763348105389034831381607519427872819735052750376719383812473081415096360867340158428371353702640632449827967163188043812193288449328058464005995046093112575926165337330100634707115160053682715014464686531460025493602539343245166620098362467196933484413717749680188294435582266877493265037758875197256932099061961217414581388227153472347319505899534413848174322474743198535953826086266146686256066319093589456135923631361106367290236939056758783671975582829257390514211329195992209734175732361974503874578275698611819911236908050184158
Loading…
Cancel
Save