-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay03_2.swift
executable file
·154 lines (137 loc) · 3.88 KB
/
Day03_2.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import Foundation
import AppKit
extension Collection {
public subscript(safe index: Index) -> Element? {
indices.contains(index) ? self[index] : nil
}
}
extension Sequence<UInt> {
func sum() -> Element {
reduce(into: 0, +=)
}
}
extension Sequence<UInt> {
func product() -> Element {
reduce(into: 1, *=)
}
}
extension Array {
mutating func prepend(_ element: Element) {
insert(element, at: 0)
}
}
extension [String] {
subscript(safeLeftSliceFrom index: Int) -> ArraySlice<String>? {
let newIndex = index - 1
guard self[safe: newIndex] != nil else { return nil }
return self[startIndex...newIndex]
}
subscript(safeRightSliceFrom index: Int) -> ArraySlice<String>? {
let newIndex = index + 1
let realEndIndex = endIndex - 1
guard self[safe: newIndex] != nil else { return nil }
return self[newIndex...realEndIndex]
}
}
extension [[String]] {
subscript(_ indexPath: IndexPath) -> String? {
self[safe: indexPath.section]?[safe: indexPath.item]
}
}
func solve(for input: String) -> UInt {
func isGear(_ element: String) -> Bool {
element == "*"
}
func isDigit(_ element: String) -> Bool {
UInt(element) != nil
}
func fillDigits(
into buffer: inout [String],
line: some BidirectionalCollection<String>,
fillOperation: (inout [String], String, _ indexCount: Int) -> Void
) {
for (index, element) in line.enumerated() {
if isDigit(element) {
fillOperation(&buffer, element, index)
} else {
return
}
}
}
var parts = [[String]]()
for line in input.components(separatedBy: .newlines) {
var _line = [String]()
for char in line {
_line.append(String(char))
}
parts.append(_line)
}
var alreadyMarkedIndices = Set<IndexPath>()
func findNumber(
at indexPath: IndexPath,
searchedItem: String,
searchedSection: [String]
) -> UInt {
alreadyMarkedIndices.insert(indexPath)
var rawDigits = [searchedItem]
if let leftPart = searchedSection[safeLeftSliceFrom: indexPath.item] {
fillDigits(into: &rawDigits, line: leftPart.reversed()) { buff, el, indexCount in
buff.prepend(el)
let i = IndexPath(item: indexPath.item - 1 - indexCount, section: indexPath.section)
alreadyMarkedIndices.insert(i)
}
}
if let rightPart = searchedSection[safeRightSliceFrom: indexPath.item] {
fillDigits(into: &rawDigits, line: rightPart) { buff, el, indexCount in
buff.append(el)
let i = IndexPath(item: indexPath.item + 1 + indexCount, section: indexPath.section)
alreadyMarkedIndices.insert(i)
}
}
return UInt(rawDigits.joined())!
}
var numbers = [UInt]()
for (sectionIndex, section) in parts.enumerated() {
for (itemIndex, item) in section.enumerated() where isGear(item) {
var localNumbers = [UInt]()
let lookupIndexPaths = [
IndexPath(item: itemIndex-1, section: sectionIndex-1),
IndexPath(item: itemIndex, section: sectionIndex-1),
IndexPath(item: itemIndex+1, section: sectionIndex-1),
IndexPath(item: itemIndex-1, section: sectionIndex),
IndexPath(item: itemIndex+1, section: sectionIndex),
IndexPath(item: itemIndex-1, section: sectionIndex+1),
IndexPath(item: itemIndex, section: sectionIndex+1),
IndexPath(item: itemIndex+1, section: sectionIndex+1),
]
for indexPath in lookupIndexPaths {
if
!alreadyMarkedIndices.contains(indexPath),
let searchedSection = parts[safe: indexPath.section],
let searchedItem = searchedSection[safe: indexPath.item],
isDigit(searchedItem)
{
let number = findNumber(
at: indexPath,
searchedItem: searchedItem,
searchedSection: searchedSection
)
localNumbers.append(number)
}
}
if localNumbers.count == 2 {
numbers.append(localNumbers.product())
}
}
}
return numbers.sum()
}
do {
let text = try String(
contentsOf: URL(fileURLWithPath: "Inputs/day03.txt"),
encoding: .utf8
)
print(solve(for: text))
} catch {
print("Error reading file: \(error)")
}