nami
This commit is contained in:
@@ -1,27 +1,17 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
enum Color: Character, CustomStringConvertible {
|
typealias Color = Character
|
||||||
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]]) {
|
func readInput(_ filePath: String) throws -> ([[Color]], [[Color]]) {
|
||||||
let content = try String(contentsOfFile: filePath, encoding: .ascii)
|
let content = try String(contentsOfFile: filePath, encoding: .ascii)
|
||||||
let lines = content.split(separator: "\n")
|
let lines = content.split(separator: "\n")
|
||||||
let towels = lines[0].split(separator: ", ").map { $0.compactMap(Color.init) }
|
let towels = lines[0].split(separator: ", ").map(Array.init)
|
||||||
let patterns = lines[1..<lines.count].map { $0.compactMap(Color.init) }
|
let patterns = lines[1..<lines.count].map(Array.init)
|
||||||
return (towels, patterns)
|
return (towels, patterns)
|
||||||
}
|
}
|
||||||
|
|
||||||
var cache: [[Color]: Bool] = [:]
|
var cache: [[Color]: Bool] = [[]: true]
|
||||||
func canMatch(_ pattern: [Color], _ towels: [[Color]]) -> Bool {
|
func canMatch(_ pattern: [Color], _ towels: [[Color]]) -> Bool {
|
||||||
if pattern.count == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if let cached = cache[pattern] {
|
if let cached = cache[pattern] {
|
||||||
return cached
|
return cached
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user