-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay12.swift
More file actions
42 lines (33 loc) · 1.17 KB
/
Day12.swift
File metadata and controls
42 lines (33 loc) · 1.17 KB
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 Day12: Day {
let title = "Garden Groups"
var rawInput: String?
func part1() throws -> Int {
let board = input().lines.map(\.characters)
return GridSequence(board)
.reduce(into: (path: Set<Position>(), answer: 0)) { result, position in
let oldCountPath = result.path.count
// swiftlint:disable:next force_unwrapping
result.answer += dfs(position, board[position]!, &result.path, board) * (result.path.count - oldCountPath)
}
.answer
}
func part2() throws -> Int {
-1
}
private func dfs(_ position: Position, _ target: Character, _ path: inout Set<Position>, _ board: [[Character]]) -> Int {
guard
0..<board.count ~= position.y,
0..<board[0].count ~= position.x,
board[position] == target
else { return 1 }
guard
!path.contains(position)
else { return 0 }
path.insert(position)
return [Direction.up, .down, .left, .right]
.map { dfs(position.offset($0), target, &path, board) }
.sum
}
}