Skip to content

Commit d9873a8

Browse files
committed
Cleanup
1 parent 1cf5322 commit d9873a8

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

19.swift

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,23 @@ func readParts() -> [Part] {
116116
}
117117

118118
func process(workflows: Workflows, parts: [Part]) -> Int {
119-
parts.filter({ doesAccept(part: $0, workflows: workflows)}).map { p in
120-
p.x + p.m + p.a + p.s
121-
}.reduce(0, +)
119+
parts
120+
.filter { p in isAccepted(workflows: workflows, part: p) }
121+
.map { p in p.x + p.m + p.a + p.s }
122+
.reduce(0, +)
122123
}
123124

124-
func doesAccept(part: Part, workflows: Workflows) -> Bool {
125-
var workflowName = "in"
125+
func isAccepted(workflows: Workflows, part: Part) -> Bool {
126+
var workflow = "in"
126127
next: while true {
127-
let workflow = workflows[workflowName]!
128-
for rule in workflow {
128+
for rule in workflows[workflow]! {
129129
if let condition = rule.condition, !condition.isValid(part: part) {
130130
} else {
131131
switch rule.action {
132132
case .accept: return true
133133
case .reject: return false
134-
case .send(let wfn):
135-
workflowName = wfn
134+
case .send(let newWorkflow):
135+
workflow = newWorkflow
136136
continue next
137137
}
138138
}
@@ -141,25 +141,26 @@ func doesAccept(part: Part, workflows: Workflows) -> Bool {
141141
}
142142

143143
func filterRanges(workflows: Workflows) -> Int {
144-
let attributes: [Attribute] = [.x, .m, .a, .s]
145-
let attributeRanges = Dictionary(uniqueKeysWithValues: attributes.map { ($0, 1...4000) })
144+
let attributeRanges: AttributeRanges =
145+
Dictionary(uniqueKeysWithValues: [.x, .m, .a, .s].map { ($0, 1...4000) })
146146
var pending = [("in", attributeRanges)]
147147
var acceptedCount = 0
148148

149-
nextPending: while let (workflow, _attributeRanges) = pending.popLast() {
150-
var attributeRanges = _attributeRanges
149+
nextPending: while let (workflow, attributeRanges) = pending.popLast() {
150+
var attributeRanges = attributeRanges
151151
for rule in workflows[workflow]! {
152152
if let condition = rule.condition {
153153
// Conditional rule, will cause attributeRanges to split.
154154

155-
// Find the part to which this rule applies.
156-
let newAttributeRanges = transformed(
157-
attributeRanges: attributeRanges,
158-
attribute: condition.attribute) {
159-
$0.clamped(to: condition.validRange)
160-
}
155+
// Find the range to which this rule applies.
156+
let attribute = condition.attribute
157+
let range = attributeRanges[attribute]!
158+
let newRange = range.clamped(to: condition.validRange)
161159

162-
// Process the part to which the condition applied.
160+
// Create a new set of attribute ranges with this range, and
161+
// apply the rule's action to it.
162+
var newAttributeRanges = attributeRanges
163+
newAttributeRanges[attribute] = newRange
163164
switch rule.action {
164165
case .reject:
165166
break
@@ -169,7 +170,8 @@ func filterRanges(workflows: Workflows) -> Int {
169170
pending.append((newWorkflow, newAttributeRanges))
170171
}
171172

172-
// For the parts that we didn't process here
173+
// There will be a leftover range, possibly empty. Continue
174+
// processing it if it is not empty.
173175
let remaining = remainingNonEmptyRanges(
174176
range: attributeRanges[condition.attribute]!,
175177
validRange: condition.validRange)
@@ -185,7 +187,7 @@ func filterRanges(workflows: Workflows) -> Int {
185187
fatalError("found 2 remaining entries \(remaining)")
186188
}
187189
} else {
188-
// Unconditional rule, always a match, so applies to the whole ranges
190+
// Unconditional rule, always a match, so consumes the whole ranges
189191
switch rule.action {
190192
case .reject:
191193
break
@@ -194,11 +196,11 @@ func filterRanges(workflows: Workflows) -> Int {
194196
case .send(let newWorkflow):
195197
pending.append((newWorkflow, attributeRanges))
196198
}
197-
// Done processing this rule
198199
continue nextPending
199200
}
200201
}
201202
}
203+
202204
return acceptedCount
203205
}
204206

@@ -208,15 +210,6 @@ func combinations(attributeRanges: AttributeRanges) -> Int {
208210
attributeRanges.values.reduce(1, { $0 * $1.count })
209211
}
210212

211-
func transformed(
212-
attributeRanges:AttributeRanges, attribute: Attribute,
213-
transform: (ClosedRange<Int>) -> ClosedRange<Int>
214-
) -> AttributeRanges {
215-
var copy = attributeRanges
216-
copy[attribute] = transform(attributeRanges[attribute]!)
217-
return copy
218-
}
219-
220213
func remainingNonEmptyRanges(range: ClosedRange<Int>, validRange: ClosedRange<Int>
221214
) -> [ClosedRange<Int>] {
222215
var result = [ClosedRange<Int>]()

0 commit comments

Comments
 (0)