generated from swiftlang/swift-aoc-starter-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2023Day12.swift
115 lines (96 loc) · 3.05 KB
/
Y2023Day12.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
final class Y2023Day12: AdventDay {
private var regex: Regex<(Substring, Substring)> { /\.?(\?*#*\?*#*)\.?/ }
let data: String
required init(data: String) {
self.data = data
}
func part1() -> Any {
lines()
.map { arrangements($0) }
.sum
}
func part2() -> Any {
lines()
.map { $0.unfolded }
.map { arrangements($0) }
.reduce(0, +)
}
private func lines() -> [Line] {
data
.components(separatedBy: .newlines)
.filter { !$0.isEmpty }
.map { newline in
let components = newline.components(separatedBy: .whitespaces)
return Line(
conditions: components[0],
sizes: components[1].components(separatedBy: ",").map { try! $0.toInteger() }
)
}
}
nonisolated(unsafe) private var cache = [Line: Int]()
private func arrangements(_ line: Line) -> Int {
if let cached = cache[line] {
return cached
} else {
let value = _arrangements(line)
cache[line] = value
return value
}
}
private func _arrangements(_ line: Line) -> Int {
if line.sizes.isEmpty {
return line.conditions.contains("#") ? 0 : 1
}
if line.conditions.isEmpty {
return 0
}
let ch = line.conditions.first!
let group = line.sizes.first!
var sum = 0
if ch == "#" {
sum = pound(line, group)
} else if ch == "." {
sum = dot(line)
} else if ch == "?" {
sum = dot(line) + pound(line, group)
}
return sum
}
private func dot(_ line: Line) -> Int {
arrangements(
Line(
conditions: String(line.conditions.dropFirst()),
sizes: line.sizes
)
)
}
private func pound(_ line: Line, _ group: Int) -> Int {
let thisGroup = line.conditions
.prefix(group)
.replacingOccurrences(of: "?", with: "#")
if thisGroup != String(repeating: "#", count: group) {
return 0
}
if line.conditions.count == group {
return line.sizes.count == 1 ? 1 : 0
}
if "?.".contains(line.conditions.charAt(group)) {
return arrangements(
Line(
conditions: String(line.conditions.dropFirst(group + 1)),
sizes: Array(line.sizes.dropFirst())
)
)
}
return 0
}
}
struct Line: Hashable {
let conditions: String
let sizes: [Int]
var unfolded: Line {
let conditions = [String](repeating: self.conditions, count: 5).joined(separator: "?")
let sizes = [[Int]](repeating: self.sizes, count: 5).flatMap { $0 }
return Line(conditions: conditions, sizes: sizes)
}
}