Refactor: Let path decide whether a room can be visited
This commit is contained in:
parent
0e3b825b3b
commit
9f919904ab
1 changed files with 11 additions and 13 deletions
22
day12.swift
22
day12.swift
|
@ -21,8 +21,12 @@ struct Day12: Puzzle {
|
||||||
struct Path {
|
struct Path {
|
||||||
var rooms: [Int] = []
|
var rooms: [Int] = []
|
||||||
|
|
||||||
func appending(_ room: Int) -> Path {
|
func appending(_ room: Int, small: Bool) -> Path? {
|
||||||
Path(rooms: rooms + [room])
|
guard !small || !rooms.contains(room) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return Path(rooms: rooms + [room])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,19 +45,13 @@ struct Day12: Puzzle {
|
||||||
return [continuing]
|
return [continuing]
|
||||||
}
|
}
|
||||||
|
|
||||||
let current = continuing.appending(from)
|
guard let current = continuing.appending(from, small: small.contains(from)) else {
|
||||||
var result: [Path] = []
|
return []
|
||||||
|
|
||||||
for next in neighbors(of: from) {
|
|
||||||
if small.contains(next) && current.rooms.contains(next) {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let nextPaths = findPaths(from: next, to: to, continuing: current)
|
return neighbors(of: from).reduce(into: []) { partialResult, next in
|
||||||
result.append(contentsOf: nextPaths)
|
partialResult += findPaths(from: next, to: to, continuing: current)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue