From 7a3715f1f417a0984a4958b67c052830af6b1d46 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 19 Dec 2020 13:20:10 +0100 Subject: [PATCH] Cleanup --- day19/main.swift | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/day19/main.swift b/day19/main.swift index 38a6a7f..4ce7012 100644 --- a/day19/main.swift +++ b/day19/main.swift @@ -75,7 +75,7 @@ extension Scanner { let rules = scanner.parseRuleSet() extension Rule { - func matches(rules: RuleSet, index: Int, _ s: Substring) -> Set { + func matches(rules: RuleSet, _ s: Substring) -> Set { switch self { case .character(let ch): if s.first == ch { @@ -85,28 +85,22 @@ extension Rule { return [] case let .sequence(first, second): - let firstMatches = first.matches(rules: rules, index: index, s) - let result = Set(firstMatches.flatMap { x -> Set in - let part = second.matches(rules: rules, index: index, x) - return part - }) - - return result + let firstMatches = first.matches(rules: rules, s) + return Set(firstMatches.flatMap { second.matches(rules: rules, $0) }) case let .alternative(first, second): - let firstMatch = first.matches(rules: rules, index: index, s) - let secondMatch = second.matches(rules: rules, index: index, s) + let firstMatch = first.matches(rules: rules, s) + let secondMatch = second.matches(rules: rules, s) - let result = firstMatch.union(secondMatch) - return result + return firstMatch.union(secondMatch) 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 { - let result = matches(rules: rules, index: index, s[...]) + func matches(rules: RuleSet, _ s: String) -> Bool { + let result = matches(rules: rules, s[...]) return result.contains("") } } @@ -135,7 +129,7 @@ let messages = scanner.readLines() 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 = """ 8: 42 | 42 8 @@ -150,4 +144,4 @@ let ruleUpdates = changeScanner.parseRuleSet() 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)