-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2022_day_17.swift
229 lines (198 loc) · 5.21 KB
/
aoc2022_day_17.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import Foundation
let input = """
>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>
"""
let inputStrings = input.split(separator: "\n").map({ String($0) })
let inputString = inputStrings.first ?? ""
enum Move {
case left
case right
init?(_ str: Character) {
if str == ">" {
self = .right
} else if str == "<" {
self = .left
} else {
return nil
}
}
}
enum Shape {
case minus
case plus
case angle
case line
case square
var next: Shape {
switch self {
case .minus:
return .plus
case .plus:
return .angle
case .angle:
return .line
case .line:
return .square
case .square:
return .minus
}
}
var shape: [[String]] {
switch self {
case .minus:
return [[".", ".", "@", "@", "@", "@", "."]]
case .plus:
return [[".", ".", ".", "@", ".", ".", "."],
[".", ".", "@", "@", "@", ".", "."],
[".", ".", ".", "@", ".", ".", "."]]
case .angle:
return [[".", ".", ".", ".", "@", ".", "."],
[".", ".", ".", ".", "@", ".", "."],
[".", ".", "@", "@", "@", ".", "."]]
case .line:
return [[".", ".", "@", ".", ".", ".", "."],
[".", ".", "@", ".", ".", ".", "."],
[".", ".", "@", ".", ".", ".", "."],
[".", ".", "@", ".", ".", ".", "."]]
case .square:
return [[".", ".", "@", "@", ".", ".", "."],
[".", ".", "@", "@", ".", ".", "."]]
}
}
var numberOfLines: Int {
let index = matrix.firstIndex(where: { $0.contains("@") }) ?? 0
switch self {
case .minus:
return 1 + index
case .plus:
return 3 + index
case .angle:
return 3 + index
case .line:
return 4 + index
case .square:
return 2 + index
}
}
}
let moves = inputString.compactMap({ Move($0) })
var moveIndex = 0 {
didSet {
if moveIndex == moves.count {
moveIndex = 0
}
}
}
var width = 7
let emptyLine = Array<String>(repeating: ".", count: width)
var blocksCount = 2022
var counter = 0
var matrix: [[String]] = []
var block = Shape.minus
while counter < blocksCount {
matrix = block.shape + [emptyLine, emptyLine, emptyLine] + matrix
moveBlock()
block = block.next
counter += 1
}
func moveBlock() {
moveTo(moves[moveIndex])
moveIndex += 1
while canMoveDown() {
moveDown()
moveTo(moves[moveIndex])
moveIndex += 1
}
finishMoving()
}
func moveDown() {
var index = block.numberOfLines - 1
while index >= 0 {
for j in 0..<width {
if matrix[index][j] == "@" {
matrix[index][j] = "."
matrix[index + 1][j] = "@"
}
}
index -= 1
}
if matrix.first?.contains("#") == false {
matrix.removeFirst()
}
}
func moveTo(_ direction: Move) {
if direction == .right && canMoveRight() {
for line in 0..<block.numberOfLines {
var index = width - 2
while index >= 0 {
if matrix[line][index] == "@" {
matrix[line][index] = "."
matrix[line][index + 1] = "@"
}
index -= 1
}
}
}
if direction == .left && canMoveLeft() {
for line in 0..<block.numberOfLines {
var index = 1
while index < width {
if matrix[line][index] == "@" {
matrix[line][index] = "."
matrix[line][index - 1] = "@"
}
index += 1
}
}
}
}
func canMoveDown() -> Bool {
let lines = matrix.prefix(block.numberOfLines + 1)
guard block.numberOfLines + 1 == lines.count else { return false }
for index in 0..<lines.count - 1 {
for j in 0..<width {
if lines[index][j] == "@" && lines[index + 1][j] == "#" {
return false
}
}
}
return true
}
func canMoveRight() -> Bool {
let lines = matrix.prefix(block.numberOfLines)
for index in 0..<lines.count {
if lines[index][width - 1] == "@" {
return false
}
for j in 0..<width - 1 {
if lines[index][j] == "@" && lines[index][j + 1] == "#" {
return false
}
}
}
return true
}
func canMoveLeft() -> Bool {
let lines = matrix.prefix(block.numberOfLines)
for index in 0..<lines.count {
if lines[index][0] == "@" {
return false
}
for j in 1..<width {
if lines[index][j] == "@" && lines[index][j - 1] == "#" {
return false
}
}
}
return true
}
func finishMoving() {
for index in 0..<block.numberOfLines {
for j in 0..<width {
if matrix[index][j] == "@" {
matrix[index][j] = "#"
}
}
}
}
print(matrix.count)