-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.swift
52 lines (40 loc) · 1.5 KB
/
Day16.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import AOCCore
import Foundation
struct Day16: Day {
let title = "Reindeer Maze"
var rawInput: String?
func part1() throws -> Int {
let board = input().lines.map(\.characters)
let startPosition = Position(y: board.count - 2, x: 1)
let endPosition = Position(y: 1, x: board[0].count - 2)
var answer = 0
var visited: Set<Position> = []
var queue = PriorityQueue<(position: Position, direction: Direction), Int>()
queue.append((startPosition, .up), 1000)
while !queue.isEmpty {
let ((currentPosition, currentDirection), currentCost) = queue.removeFirst()
guard
0..<board.count ~= currentPosition.y,
0..<board[0].count ~= currentPosition.x,
board[currentPosition] != "#",
!visited.contains(currentPosition)
else { continue }
if currentPosition == endPosition {
answer = currentCost
break
}
visited.insert(currentPosition)
[Direction.up, .down, .left, .right].forEach {
let newPosition = currentPosition.offset($0)
if !visited.contains(newPosition) {
let additionalCost = currentDirection == $0 ? 0 : 1000
queue.append((newPosition, $0), currentCost + additionalCost + 1)
}
}
}
return answer
}
func part2() throws -> Int {
return -1
}
}