advent-of-code/12022/10/10_2.lua

42 lines
868 B
Lua

io.input("test2")
io.input("input")
-- for part 2
function pixel( cycle, spx )
local cx = (cycle-1)%40
local s = (cx == 0) and "\n" or ""
local ch = (cx>=spx-1) and (cx<=spx+1) and "#" or "."
return s .. ch
end
local cycle = 1
local x = 1
local strengths = 0 -- part 1
local screen = "" -- part 2
-- process
for line in io.lines() do
local ins, num = string.match(line, "^(%a+) (-?%d+)$")
-- part 1
local period = (cycle-20)%40
if period == 0 then
print(cycle,x)
strengths = strengths + cycle*x
elseif num and period == 39 then
--print(cycle,x)
strengths = strengths + (cycle+1)*x
end
-- part 2
screen = screen .. pixel(cycle, x)
if num then -- addx
screen = screen .. pixel(cycle+1, x)
cycle = cycle + 1
x = x + tonumber(num)
end
cycle = cycle + 1
end
print("part 1", strengths)
print("part 2",screen)