From 3057dfabac0f9a0b7ef92e382f7cc9d599a2afdc Mon Sep 17 00:00:00 2001 From: Dory Date: Wed, 18 Dec 2024 22:22:02 -0800 Subject: [PATCH] d19 --- day19/d19p1.swift | 41 +++++ day19/d19p2.swift | 37 +++++ day19/input.txt | 402 ++++++++++++++++++++++++++++++++++++++++++++++ day19/test.txt | 10 ++ 4 files changed, 490 insertions(+) create mode 100644 day19/d19p1.swift create mode 100644 day19/d19p2.swift create mode 100644 day19/input.txt create mode 100644 day19/test.txt diff --git a/day19/d19p1.swift b/day19/d19p1.swift new file mode 100644 index 0000000..91f794e --- /dev/null +++ b/day19/d19p1.swift @@ -0,0 +1,41 @@ +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) } +} + +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.. Bool { + if pattern.count == 0 { + return true + } + if let cached = cache[pattern] { + return cached + } + for towel in towels { + if pattern.starts(with: towel) { + if canMatch(Array(pattern[towel.count.. ([[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 + } + let r = towels + .filter { pattern.starts(with: $0) } + .map { matches(Array(pattern[$0.count..