Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local program = {}
- for line in io.lines(arg[1]) do
- local code, amount = line:match("(%w+) (%-?%d+)")
- if code and amount then
- table.insert(program, { code = code, amount = tonumber(amount) })
- else
- table.insert(program, { code = "noop", amount = nil })
- end
- end
- local steps = {[20] = true, [60] = true, [100] = true, [140] = true, [180] = true, [220] = true}
- local grid = {}
- local X = 1
- local cycle = 1
- local ind = 1
- local total = 0
- while cycle <= 240 do
- if program[ind].code == "noop" then
- local row = math.floor((cycle - 1) / 40)
- local col = cycle - 40 * row - 1
- grid[row] = grid[row] or {}
- if (col == X - 1) or (col == X) or (col == X + 1) then
- grid[row][col] = "#"
- else
- grid[row][col] = "."
- end
- if steps[cycle] then
- total = total + cycle * X
- end
- cycle = cycle + 1
- ind = ind + 1
- else
- for _ = 1, 2 do
- local row = math.floor((cycle - 1) / 40)
- local col = cycle - 40 * row - 1
- grid[row] = grid[row] or {}
- if (col == X - 1) or (col == X) or (col == X + 1) then
- grid[row][col] = "#"
- else
- grid[row][col] = "."
- end
- if steps[cycle] then
- total = total + cycle * X
- end
- cycle = cycle + 1
- end
- X = X + program[ind].amount
- ind = ind + 1
- end
- end
- print(total)
- for y = 0, 5 do
- local s = ""
- for x = 0, 39 do
- s = s .. grid[y][x]
- end
- print(s)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement