-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2020_day16.lua
More file actions
92 lines (88 loc) · 1.98 KB
/
Copy path2020_day16.lua
File metadata and controls
92 lines (88 loc) · 1.98 KB
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
local rulenames = {}
local rules = {}
local myticket = {}
local tickets = {}
local a, b, c = getinput():match("(.+)\n\nyour ticket:(.+)\n\nnearby tickets:(.+)")
for v in a:gmatch("[^\n]+") do
rulenames[#rulenames+1] = v:match("[^:]+")
local nums = {}
for n in v:gmatch("%d+") do
nums[#nums+1] = tonumber(n)
end
rules[#rules+1] = nums
end
for n in b:gmatch("%d+") do
myticket[#myticket+1] = tonumber(n)
end
for v in c:gmatch("[^\n]+") do
local t = {}
for n in v:gmatch("%d+") do
t[#t+1] = tonumber(n)
end
tickets[#tickets+1] = t
end
local ranges = {}
for i = 1, #myticket do
ranges[i] = {low = math.huge, high = -math.huge}
end
for _, t in pairs(tickets) do
for i = 1, #t do
ranges[i].low = math.min(ranges[i].low, t[i])
ranges[i].high = math.max(ranges[i].high, t[i])
end
end
local errorrate = 0
local invalids = {}
for index, t in pairs(tickets) do
for i = 1, #t do
local valid = false
for _, r in pairs(rules) do
if (t[i] >= r[1] and t[i] <= r[2]) or (t[i] >= r[3] and t[i] <= r[4]) then
valid = true
break
end
end
if not valid then
errorrate = errorrate + t[i]
if invalids[#invalids] ~= index then
invalids[#invalids+1] = index
end
end
end
end
print(errorrate) -- part 1
for i = #invalids, 1, -1 do
table.remove(tickets, invalids[i])
end
local product = 1
for _ = 1, #myticket do
for i = 1, #myticket do
local possible = {}
for index, r in pairs(rules) do
local valid = true
for _, t in pairs(tickets) do
if not ((t[i] >= r[1] and t[i] <= r[2]) or (t[i] >= r[3] and t[i] <= r[4])) then
valid = false
break
end
end
if valid then
possible[#possible+1] = index
end
end
if #possible == 1 then
local rule = possible[1]
if rulenames[rule]:match("^departure") then
product = product * myticket[i]
end
for _, t in pairs(tickets) do
table.remove(t, i)
end
table.remove(myticket, i)
table.remove(rulenames, rule)
table.remove(rules, rule)
break
end
end
end
print(product) -- part 2