2021-12-05 12:17:39 +01:00
|
|
|
#!/usr/bin/env luajit
|
|
|
|
|
2021-12-04 22:49:16 +01:00
|
|
|
function split(text, sep)
|
|
|
|
local parts = {}
|
|
|
|
local start = 0
|
|
|
|
local from, to = text:find(sep)
|
|
|
|
while from ~= nil do
|
|
|
|
table.insert(parts, text:sub(start, from - 1))
|
|
|
|
text = text:sub(to + 1)
|
|
|
|
from, to = text:find(sep)
|
|
|
|
end
|
|
|
|
table.insert(parts, text)
|
|
|
|
return parts
|
|
|
|
end
|
|
|
|
|
|
|
|
local f = io.open(arg[3])
|
|
|
|
local numbers = split(f:read("*l"), ",")
|
|
|
|
local line = f:read("*l")
|
|
|
|
local boards = {}
|
|
|
|
while line ~= nil do
|
|
|
|
local board = {}
|
|
|
|
for r = 1, 5 do
|
|
|
|
line = f:read("*l")
|
|
|
|
while line:byte(1) == string.byte(" ", 1) do
|
|
|
|
line = line:sub(2)
|
|
|
|
end
|
|
|
|
board[r] = split(line, " +")
|
|
|
|
end
|
|
|
|
table.insert(boards, board)
|
|
|
|
line = f:read("*l")
|
|
|
|
end
|
|
|
|
|
|
|
|
local i, complete = 0, nil
|
|
|
|
while complete == nil and i <= #numbers do
|
|
|
|
local number = numbers[i]
|
|
|
|
local tests = {}
|
|
|
|
for b, board in pairs(boards) do
|
|
|
|
for r, row in pairs(board) do
|
|
|
|
for c, col in pairs(row) do
|
|
|
|
if col == number then
|
|
|
|
row[c] = nil
|
|
|
|
table.insert(tests, {b, r, c})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for t, test in pairs(tests) do
|
|
|
|
local board, row, col = unpack(test)
|
|
|
|
local j, rownils, colnils = 1, 0, 0
|
|
|
|
while j <= 5 and (rownils == 0 or colnils == 0) do
|
|
|
|
if boards[board][row][j] ~= nil then rownils = rownils + 1 end
|
|
|
|
if boards[board][j][col] ~= nil then colnils = colnils + 1 end
|
|
|
|
j = j + 1
|
|
|
|
end
|
|
|
|
if rownils == 0 or colnils == 0 then
|
|
|
|
complete = boards[board]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
print(numbers[i - 1])
|
|
|
|
local sum = 0
|
|
|
|
for r = 1, 5 do
|
|
|
|
for c = 1, 5 do
|
|
|
|
io.write((complete[r][c] or "nil") .. " ")
|
|
|
|
sum = sum + (complete[r][c] or 0)
|
|
|
|
end
|
|
|
|
io.write("\n")
|
|
|
|
end
|
|
|
|
print(numbers[i - 1] * sum)
|