Day 12 Part 2

This commit is contained in:
Sven Weidauer 2021-12-12 12:33:20 +01:00
parent 9f919904ab
commit 5c44a65f67

View file

@ -1,3 +1,7 @@
protocol PathProtocol {
func appending(_ room: Int, small: Bool) -> Self?
}
@main @main
struct Day12: Puzzle { struct Day12: Puzzle {
func run() { func run() {
@ -14,11 +18,13 @@ struct Day12: Puzzle {
let endIndex = rooms.firstIndex(of: "end")! let endIndex = rooms.firstIndex(of: "end")!
let paths = matrix.findPaths(from: startIndex, to: endIndex, continuing: Path()) let paths = matrix.findPaths(from: startIndex, to: endIndex, continuing: Path())
print("total paths", paths.count) print("total paths", paths.count)
let paths2 = matrix.findPaths(from: startIndex, to: endIndex, continuing: PathParth2(start: startIndex))
print("part 2:", paths2.count)
} }
struct Path { struct Path: PathProtocol {
var rooms: [Int] = [] var rooms: [Int] = []
func appending(_ room: Int, small: Bool) -> Path? { func appending(_ room: Int, small: Bool) -> Path? {
@ -30,6 +36,25 @@ struct Day12: Puzzle {
} }
} }
struct PathParth2: PathProtocol {
let start: Int
var rooms: [Int] = []
var repeatedSmall: Bool = false
func appending(_ room: Int, small: Bool) -> PathParth2? {
var rs = repeatedSmall
if small && rooms.contains(room) {
if room == start || repeatedSmall {
return nil
}
rs = true
}
return PathParth2(start: start, rooms: rooms + [room], repeatedSmall: rs)
}
}
struct Matrix { struct Matrix {
var data: [Bool] var data: [Bool]
let size: Int let size: Int
@ -40,13 +65,13 @@ struct Day12: Puzzle {
return slice.enumerated().compactMap { $0.element ? $0.offset : nil } return slice.enumerated().compactMap { $0.element ? $0.offset : nil }
} }
func findPaths(from: Int, to: Int, continuing: Path) -> [Path] { func findPaths<Path: PathProtocol>(from: Int, to: Int, continuing: Path) -> [Path] {
guard from != to else { guard from != to else {
return [continuing] return [continuing]
} }
guard let current = continuing.appending(from, small: small.contains(from)) else { guard let current = continuing.appending(from, small: small.contains(from)) else {
return [] return []
} }
return neighbors(of: from).reduce(into: []) { partialResult, next in return neighbors(of: from).reduce(into: []) { partialResult, next in