-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.swift
42 lines (33 loc) · 1.06 KB
/
Day10.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
import AOCCore
import Foundation
struct Day10: Day {
let title = "Hoof It"
var rawInput: String?
func part1() throws -> Int {
let board = input().lines.map { $0.characters.compactMap(Int.init) }
return board.findAll(element: 0)
.map { dfs($0, 0, board) }
.map(\.unique)
.map(\.count)
.sum
}
func part2() throws -> Int {
let board = input().lines.map { $0.characters.compactMap(Int.init) }
return board.findAll(element: 0)
.map { dfs($0, 0, board) }
.map(\.count)
.sum
}
private func dfs(_ position: Position, _ target: Int, _ board: [[Int]]) -> [Position] {
guard
(0..<board.count) ~= position.y,
(0..<board[0].count) ~= position.x,
board[position] == target
else { return [] }
guard
target != 9
else { return [position] }
return [Direction.up, .down, .left, .right]
.flatMap { dfs(position.offset($0), target + 1, board) }
}
}