-
Notifications
You must be signed in to change notification settings - Fork 0
/
14b.lua
119 lines (106 loc) · 2.53 KB
/
14b.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
-- lua 14b.lua
function replace(lines, i, j, c)
lines[i] = lines[i]:sub(1, j - 1) .. c .. lines[i]:sub(j + 1)
end
function move(lines, x1, y1, x2, y2)
replace(lines, x1, y1, '.')
replace(lines, x2, y2, 'O')
end
function rotate(lines)
local n = #lines
local m = #lines[1]
local function get(i, j) return lines[i]:sub(j, j) end
for j = 1, m do
local ii = 1
for i = 1, n do
if get(i, j) == '#' then
ii = i + 1
elseif get(i, j) == 'O' then
move(lines, i, j, ii, j)
ii = ii + 1
end
end
end
for i = 1, n do
local jj = 1
for j = 1, m do
if get(i, j) == '#' then
jj = j + 1
elseif get(i, j) == 'O' then
move(lines, i, j, i, jj)
jj = jj + 1
end
end
end
for j = 1, m do
local ii = n
for i = n, 1, -1 do
if get(i, j) == '#' then
ii = i - 1
elseif get(i, j) == 'O' then
move(lines, i, j, ii, j)
ii = ii - 1
end
end
end
for i = 1, n do
local jj = m
for j = m, 1, -1 do
if get(i, j) == '#' then
jj = j - 1
elseif get(i, j) == 'O' then
move(lines, i, j, i, jj)
jj = jj - 1
end
end
end
end
function hash(lines)
local result = 0
for i = 1, #lines do
for j = 1, #lines[i] do
local c = lines[i]:sub(j, j)
result = result * 31 + string.byte(c)
end
end
return result
end
function count(lines)
local n = #lines
local result = 0
for j = 1, #lines[1] do
for i = 1, n do
if lines[i]:sub(j, j) == 'O' then
result = result + n - i + 1
end
end
end
return result
end
function solve(lines)
local prev = {}
local ans = {}
prev[hash(lines)] = 0
ans[0] = count(lines)
local iter = 1
while true do
rotate(lines)
local h = hash(lines)
if prev[h] then
local period = iter - prev[h]
local target = (1000000000 - prev[h]) % period
return ans[prev[h] + target]
end
prev[h] = iter
ans[iter] = count(lines)
iter = iter + 1
end
end
function main()
local lines = {}
for line in io.lines() do
lines[#lines + 1] = line
end
print(solve(lines))
end
main()