15b
This commit is contained in:
parent
07ac0b2fc5
commit
2239760ce2
@ -29,7 +29,6 @@ struct Coord: Hashable, CustomStringConvertible {
|
|||||||
|
|
||||||
class Day15: Runnable {
|
class Day15: Runnable {
|
||||||
let inputPath: String
|
let inputPath: String
|
||||||
var field = [Coord]()
|
|
||||||
var lastRow: Int = 0
|
var lastRow: Int = 0
|
||||||
var lastCol: Int = 0
|
var lastCol: Int = 0
|
||||||
var parts = [[ThreatLevel]]()
|
var parts = [[ThreatLevel]]()
|
||||||
@ -71,22 +70,29 @@ class Day15: Runnable {
|
|||||||
lastRow = parts.count - 1
|
lastRow = parts.count - 1
|
||||||
lastCol = (parts.first?.count ?? 0) - 1
|
lastCol = (parts.first?.count ?? 0) - 1
|
||||||
|
|
||||||
var unvisited = Set<Coord>()
|
|
||||||
// Distances to startNode
|
// Distances to startNode
|
||||||
var totalThreatLevel = [Coord: ThreatLevel]()
|
var totalThreatLevel = [Coord: ThreatLevel]()
|
||||||
for row in parts.enumerated() {
|
|
||||||
for col in row.element.indices {
|
var unvisited = Set(parts.enumerated().flatMap {row in
|
||||||
let c = Coord(row: row.offset, col: col)
|
row.element.indices.map { col in
|
||||||
unvisited.insert(c)
|
Coord(row: row.offset, col: col)
|
||||||
//totalThreatLevel[c] = Int.max
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
var threatLevelDistrubution = [ThreatLevel: Set<Coord>]()
|
||||||
let startNode = Coord(row: 0, col: 0)
|
let startNode = Coord(row: 0, col: 0)
|
||||||
totalThreatLevel[startNode] = 0
|
totalThreatLevel[startNode] = 0
|
||||||
|
threatLevelDistrubution[Int.max] = unvisited
|
||||||
|
threatLevelDistrubution[Int.max]!.remove(startNode)
|
||||||
|
threatLevelDistrubution[0] = Set([startNode])
|
||||||
|
|
||||||
while let currentNode = unvisited.min(by: { totalThreatLevel[$0, default: Int.max] < totalThreatLevel[$1, default: Int.max] }) {
|
while let threatLevel = threatLevelDistrubution.keys.min() {
|
||||||
//var currentNode = !
|
let currentNode = threatLevelDistrubution[threatLevel]!.first!
|
||||||
unvisited.remove(currentNode)
|
unvisited.remove(currentNode)
|
||||||
|
threatLevelDistrubution[threatLevel]!.remove(currentNode)
|
||||||
|
if threatLevelDistrubution[threatLevel]!.isEmpty {
|
||||||
|
threatLevelDistrubution.removeValue(forKey: threatLevel)
|
||||||
|
}
|
||||||
|
|
||||||
if neighbors[currentNode] == nil {
|
if neighbors[currentNode] == nil {
|
||||||
neighbors[currentNode] = currentNode
|
neighbors[currentNode] = currentNode
|
||||||
.neighbors()
|
.neighbors()
|
||||||
@ -103,8 +109,14 @@ class Day15: Runnable {
|
|||||||
let currentThreatLevel = totalThreatLevel[currentNode, default: Int.max]
|
let currentThreatLevel = totalThreatLevel[currentNode, default: Int.max]
|
||||||
for a in adjacent {
|
for a in adjacent {
|
||||||
let testCost = parts[a] + currentThreatLevel
|
let testCost = parts[a] + currentThreatLevel
|
||||||
if totalThreatLevel[a, default: Int.max] > testCost {
|
let currentCost = totalThreatLevel[a, default: Int.max]
|
||||||
|
if currentCost > testCost {
|
||||||
totalThreatLevel[a] = testCost
|
totalThreatLevel[a] = testCost
|
||||||
|
threatLevelDistrubution[currentCost]!.remove(a)
|
||||||
|
threatLevelDistrubution[testCost, default: Set<Coord>()].insert(a)
|
||||||
|
if threatLevelDistrubution[currentCost]!.isEmpty {
|
||||||
|
threatLevelDistrubution.removeValue(forKey: currentCost)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user