day 19 part 2 solved

This commit is contained in:
Sven Weidauer 2020-12-19 13:17:41 +01:00
parent 7e8a88629d
commit 7c7d772ff5

View file

@ -75,40 +75,30 @@ extension Scanner {
let rules = scanner.parseRuleSet() let rules = scanner.parseRuleSet()
extension Rule { extension Rule {
func matches(rules: RuleSet, index: Int, _ s: Substring) -> Substring? { func matches(rules: RuleSet, index: Int, _ s: Substring) -> Set<Substring> {
switch self { switch self {
case .character(let ch): case .character(let ch):
if s.first == ch { if s.first == ch {
return s.dropFirst() return [s.dropFirst()]
} }
return nil return []
case let .sequence(first, second): case let .sequence(first, second):
if let firstMatch = first.matches(rules: rules, index: index, s), let secondMatch = second.matches(rules: rules, index: index, firstMatch) { let firstMatches = first.matches(rules: rules, index: index, s)
return secondMatch let result = Set(firstMatches.flatMap { x -> Set<Substring> in
} let part = second.matches(rules: rules, index: index, x)
return part
})
return nil return result
case .alternative(let a, .sequence(let b, .reference(index))) where a == b:
var rest = s
while let match = a.matches(rules: rules, index: index, rest) {
rest = match
}
guard rest != s else { return nil }
return rest
case let .alternative(first, second): case let .alternative(first, second):
if let firstMatch = first.matches(rules: rules, index: index, s) { let firstMatch = first.matches(rules: rules, index: index, s)
return firstMatch let secondMatch = second.matches(rules: rules, index: index, s)
}
if let secondMatch = second.matches(rules: rules, index: index, s) { let result = firstMatch.union(secondMatch)
return secondMatch return result
}
return nil
case .reference(let index): case .reference(let index):
return rules[index]!.matches(rules: rules, index: index, s) return rules[index]!.matches(rules: rules, index: index, s)
@ -116,11 +106,8 @@ extension Rule {
} }
func matches(rules: RuleSet, index: Int, _ s: String) -> Bool { func matches(rules: RuleSet, index: Int, _ s: String) -> Bool {
if let result = matches(rules: rules, index: index, s[...]), result.isEmpty { let result = matches(rules: rules, index: index, s[...])
return true return result.contains("")
}
return false
} }
} }
@ -146,7 +133,9 @@ extension Scanner {
let messages = scanner.readLines() let messages = scanner.readLines()
print("part 1", messages.lazy.filter { rules[0]!.matches(rules: rules,index: 0, $0) }.count) let start = Rule.reference(0)
print("part 1:", messages.lazy.filter { start.matches(rules: rules,index: 0, $0) }.count)
let changedRules = """ let changedRules = """
8: 42 | 42 8 8: 42 | 42 8
@ -161,4 +150,4 @@ let ruleUpdates = changeScanner.parseRuleSet()
let newRules = rules.merging(ruleUpdates) { $1 } let newRules = rules.merging(ruleUpdates) { $1 }
print("part 2", messages.lazy.filter { rules[0]!.matches(rules: newRules, index: 0, $0) }.count) print("part 2:", messages.filter { start.matches(rules: newRules, index: 0, $0) }.count)