-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
executable file
·194 lines (180 loc) · 5.65 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/lua
function copyTable(original)
local copy = {}
for key, value in pairs(original) do
if type(value) == "table" then
copy[key] = copyTable(value)
else
copy[key] = value
end
end
return copy
end
function parseInput(filePath)
local file = io.open(filePath, "r")
local content = file:read("*a")
file:close()
local workflows = {}
local parts = {}
for line in content:gmatch("[^\r\n]+") do
if string.sub(line, 1, 1) == "{" then
local part = {}
for key, value in string.sub(line, 2, -2):gmatch("(%w+)=(%d+),?") do
part[key] = tonumber(value)
end
table.insert(parts, part)
else
local workflow = {}
workflow["name"] = string.sub(line, 0, string.find(line, "{")-1)
workflow["rules"] = {}
for rule in string.sub(line, string.find(line, "{")+1, -2):gmatch("([^,]+),?") do
table.insert(workflow["rules"], rule)
end
table.insert(workflows, workflow)
end
end
return workflows, parts
end
function findWorkflow(workflows, name)
for _, workflow in ipairs(workflows) do
if workflow["name"] == name then
return workflow
end
end
return nil
end
function applyRule(rules, part)
for _, rule in ipairs(rules) do
if string.find(rule, ">") then
local key, value, name = string.match(rule, "(%w+)>(%d+):(%w+)")
if part[key] > tonumber(value) then
return name
end
elseif string.find(rule, "<") then
local key, value, name = string.match(rule, "(%w+)<(%d+):(%w+)")
if part[key] < tonumber(value) then
return name
end
else
return rule
end
end
return nil
end
function isPartAccepted(workflows, part)
local workflowName = "in"
while true do
local workflow = findWorkflow(workflows, workflowName)
local output = applyRule(workflow["rules"], part)
if output == "R" then
return false
elseif output == "A" then
return true;
else
workflowName = output
end
end
end
function sumAcceptedParts(workflows, parts)
local total = 0
for _, part in ipairs(parts) do
if isPartAccepted(workflows, part) then
for _, value in pairs(part) do
total = total + value
end
end
end
return total
end
function applyRuleToRange(rules, stack, currentRange)
for _, rule in ipairs(rules) do
if string.find(rule, ">") then
local key, value, name = string.match(rule, "(%w+)>(%d+):(%w+)")
local min, max = currentRange[key][1], currentRange[key][2]
value = tonumber(value)
if min > value then
return name
elseif max > value then
currentRange[key] = {value+1, max}
local newRange = copyTable(currentRange)
newRange[key] = {min, value}
table.insert(stack, newRange)
return name
end
elseif string.find(rule, "<") then
local key, value, name = string.match(rule, "(%w+)<(%d+):(%w+)")
local min, max = currentRange[key][1], currentRange[key][2]
value = tonumber(value)
if max < value then
return name
elseif min < value then
currentRange[key] = {min, value-1}
local newRange = copyTable(currentRange)
newRange[key] = {value, max}
table.insert(stack, newRange)
return name
end
else
return rule
end
end
return nil
end
function processRange(workflows, stack, currentRange)
local workflowName = "in"
while true do
local workflow = findWorkflow(workflows, workflowName)
local output = applyRuleToRange(workflow["rules"], stack, currentRange)
if output == "R" then
return false
elseif output == "A" then
return true;
else
workflowName = output
end
end
end
function computeCombinations(range)
local combinations = 1
for _, values in pairs(range) do
combinations = combinations * (1 + values[2] - values[1])
end
return combinations
end
function availableCombinations(workflows)
local combinations = 0
local initialRange = {
x = {1, 4000},
m = {1, 4000},
a = {1, 4000},
s = {1, 4000},
}
local stack = {}
table.insert(stack, initialRange)
while #stack > 0 do
currentRange = table.remove(stack)
if processRange(workflows, stack, currentRange) then
combinations = combinations + computeCombinations(currentRange)
end
end
return combinations
end
function part01(filePath)
workflows, parts = parseInput(filePath)
return sumAcceptedParts(workflows, parts)
end
function part02(filePath)
workflows, _ = parseInput(filePath)
return availableCombinations(workflows)
end
function main()
assert(part01("sample.txt") == 19114, "Part 01 incorrect for sample file")
local part01output = part01("input.txt")
print("Part 01: " .. part01output)
assert(part01output == 325952, "Part 01 incorrect for input file")
assert(part02("sample.txt") == 167409079868000, "Part 02 incorrect for sample file")
local part02output = part02("input.txt")
print("Part 02: " .. part02output)
assert(part02output == 125744206494820, "Part 02 incorrect for input file")
end
main()