Cleanup
This commit is contained in:
parent
7c7d772ff5
commit
7a3715f1f4
1 changed files with 11 additions and 17 deletions
|
@ -75,7 +75,7 @@ extension Scanner {
|
||||||
let rules = scanner.parseRuleSet()
|
let rules = scanner.parseRuleSet()
|
||||||
|
|
||||||
extension Rule {
|
extension Rule {
|
||||||
func matches(rules: RuleSet, index: Int, _ s: Substring) -> Set<Substring> {
|
func matches(rules: RuleSet, _ s: Substring) -> Set<Substring> {
|
||||||
switch self {
|
switch self {
|
||||||
case .character(let ch):
|
case .character(let ch):
|
||||||
if s.first == ch {
|
if s.first == ch {
|
||||||
|
@ -85,28 +85,22 @@ extension Rule {
|
||||||
return []
|
return []
|
||||||
|
|
||||||
case let .sequence(first, second):
|
case let .sequence(first, second):
|
||||||
let firstMatches = first.matches(rules: rules, index: index, s)
|
let firstMatches = first.matches(rules: rules, s)
|
||||||
let result = Set(firstMatches.flatMap { x -> Set<Substring> in
|
return Set(firstMatches.flatMap { second.matches(rules: rules, $0) })
|
||||||
let part = second.matches(rules: rules, index: index, x)
|
|
||||||
return part
|
|
||||||
})
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
case let .alternative(first, second):
|
case let .alternative(first, second):
|
||||||
let firstMatch = first.matches(rules: rules, index: index, s)
|
let firstMatch = first.matches(rules: rules, s)
|
||||||
let secondMatch = second.matches(rules: rules, index: index, s)
|
let secondMatch = second.matches(rules: rules, s)
|
||||||
|
|
||||||
let result = firstMatch.union(secondMatch)
|
return firstMatch.union(secondMatch)
|
||||||
return result
|
|
||||||
|
|
||||||
case .reference(let index):
|
case .reference(let index):
|
||||||
return rules[index]!.matches(rules: rules, index: index, s)
|
return rules[index]!.matches(rules: rules, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func matches(rules: RuleSet, index: Int, _ s: String) -> Bool {
|
func matches(rules: RuleSet, _ s: String) -> Bool {
|
||||||
let result = matches(rules: rules, index: index, s[...])
|
let result = matches(rules: rules, s[...])
|
||||||
return result.contains("")
|
return result.contains("")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -135,7 +129,7 @@ let messages = scanner.readLines()
|
||||||
|
|
||||||
let start = Rule.reference(0)
|
let start = Rule.reference(0)
|
||||||
|
|
||||||
print("part 1:", messages.lazy.filter { start.matches(rules: rules,index: 0, $0) }.count)
|
print("part 1:", messages.lazy.filter { start.matches(rules: rules, $0) }.count)
|
||||||
|
|
||||||
let changedRules = """
|
let changedRules = """
|
||||||
8: 42 | 42 8
|
8: 42 | 42 8
|
||||||
|
@ -150,4 +144,4 @@ let ruleUpdates = changeScanner.parseRuleSet()
|
||||||
|
|
||||||
let newRules = rules.merging(ruleUpdates) { $1 }
|
let newRules = rules.merging(ruleUpdates) { $1 }
|
||||||
|
|
||||||
print("part 2:", messages.filter { start.matches(rules: newRules, index: 0, $0) }.count)
|
print("part 2:", messages.filter { start.matches(rules: newRules, $0) }.count)
|
||||||
|
|
Loading…
Add table
Reference in a new issue