From ea0fce76e538f657f1e4a0b27e465215f1ac2e52 Mon Sep 17 00:00:00 2001 From: Dory Date: Thu, 19 Dec 2024 08:42:56 -0800 Subject: [PATCH] make d19p2 shorter --- day19/d19p2.swift | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/day19/d19p2.swift b/day19/d19p2.swift index d55b029..fbfc713 100644 --- a/day19/d19p2.swift +++ b/day19/d19p2.swift @@ -1,30 +1,19 @@ import Foundation -enum Color: Character, CustomStringConvertible { - case r = "r" - case g = "g" - case u = "u" - case w = "w" - case b = "b" - var description: String { return String(self.rawValue) } -} +typealias Color = Character func readInput(_ filePath: String) throws -> ([[Color]], [[Color]]) { let content = try String(contentsOfFile: filePath, encoding: .ascii) let lines = content.split(separator: "\n") - let towels = lines[0].split(separator: ", ").map { $0.compactMap(Color.init) } - let patterns = lines[1.. Int { - if pattern.count == 0 { - return 1 - } - if let cached = cache[pattern] { - return cached - } + if pattern.count == 0 { return 1 } + if let cached = cache[pattern] { return cached } let r = towels .filter { pattern.starts(with: $0) } .map { matches(Array(pattern[$0.count.. Int { } let (towels, patterns) = try readInput(CommandLine.arguments[1]) -print(patterns.reduce(0) { s, p in s + matches(p, towels) }) +print(patterns.map{ matches($0, towels) }.reduce(0, +))