blob: 8c2d155a4c2b5dce1fedf17b04d67fb6da3ff079 (
plain)
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
|
#!/usr/bin/env lua
require("ext")
local items = {}
for start, finish in pairs({a = 'z', A = 'Z'}) do
for k = start:byte(), finish:byte() do
items[string.char(k)] = true
end
end
local groups = {}
for line in io.lines() do
local expanded = {}
line:gsub("(%w)",function(w) expanded[w] = true end)
table.insert(groups,expanded)
end
local common = {}
for i = 1,#groups,3 do
for item, _ in pairs(items) do
if groups[i][item] and groups[i + 1][item] and groups[i + 2][item] then
table.insert(common, item)
break
end
end
end
local sum = 0
for _, p in pairs(common) do
if p >= 'a' and p <= 'z' then
sum = sum + (p:byte() - ('a'):byte() + 1)
elseif p >= 'A' and p <= 'Z' then
sum = sum + (p:byte() - ('A'):byte() + 1) + 26
end
end
print(sum)
|