-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2022_day_9.swift
138 lines (116 loc) · 3.97 KB
/
aoc2022_day_9.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
let input = """
R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20
"""
let inputStrings = input.split(separator: "\n", omittingEmptySubsequences: false).map({ String($0) })
// helpers
enum Command {
case lef(Int)
case righ(Int)
case up(Int)
case down(Int)
init?(_ string: String) {
let split = string.split(separator: " ")
let value = Int(String(split[1])) ?? 0
switch split[0] {
case "R":
self = .righ(value)
case "L":
self = .lef(value)
case "U":
self = .up(value)
case "D":
self = .down(value)
default:
return nil
}
}
}
func executeCommand(_ command: Command) {
switch command {
case let .righ(value), let .lef(value), let .up(value), let .down(value):
for _ in 0..<value {
executeOne(command)
}
}
}
func executeOne(_ command: Command) {
switch command {
case .righ:
tails[0] = (tails[0].0 + 1, tails[0].1)
case .lef:
tails[0] = (tails[0].0 - 1, tails[0].1)
case .up:
tails[0] = (tails[0].0, tails[0].1 + 1)
case .down:
tails[0] = (tails[0].0, tails[0].1 - 1)
}
for index in 1..<tails.count {
adjustTailPartTwo(index: index)
}
tailPositions.insert([tails[tails.count - 1].0, tails[tails.count - 1].1])
}
func newPoint(a: Int, b: Int) -> Int {
return a + b - ((a + b) / 2)
}
func adjustTailPartTwo(index: Int) {
if abs(tails[index].1 - tails[index - 1].1) <= 1 && abs(tails[index].0 - tails[index - 1].0) <= 1 {
return
}
if tails[index - 1].0 == tails[index].0 &&
abs(tails[index].1 - tails[index - 1].1) == 2 {
tails[index] = (tails[index].0, newPoint(a: tails[index].1, b: tails[index - 1].1))
} else if tails[index - 1].1 == tails[index].1 &&
abs(tails[index].0 - tails[index - 1].0) == 2 {
tails[index] = (newPoint(a: tails[index].0, b: tails[index - 1].0), tails[index].1)
} else if abs(tails[index].0 - tails[index - 1].0) > 1 &&
abs(tails[index].1 - tails[index - 1].1) == 1 {
if tails[index].0 < tails[index - 1].0 {
tails[index] = (tails[index].0 + 1, tails[index - 1].1)
}
if tails[index - 1].0 < tails[index].0 {
tails[index] = (tails[index - 1].0 + 1, tails[index - 1].1)
}
} else if abs(tails[index].1 - tails[index - 1].1) > 1 &&
abs(tails[index].0 - tails[index - 1].0) == 1 {
if tails[index].1 < tails[index - 1].1 {
tails[index] = (tails[index - 1].0, tails[index].1 + 1)
}
if tails[index - 1].1 < tails[index].1 {
tails[index] = (tails[index - 1].0, tails[index - 1].1 + 1)
}
} else if abs(tails[index].0 - tails[index - 1].0) == 2 &&
abs(tails[index].1 - tails[index - 1].1) == 2 {
if tails[index].0 < tails[index - 1].0 && tails[index].1 < tails[index - 1].1 {
tails[index] = (tails[index].0 + 1 , tails[index].1 + 1)
}
if tails[index].0 < tails[index - 1].0 && tails[index].1 > tails[index - 1].1 {
tails[index] = (tails[index].0 + 1 , tails[index].1 - 1)
}
if tails[index].0 > tails[index - 1].0 && tails[index].1 > tails[index - 1].1 {
tails[index] = (tails[index].0 - 1 , tails[index].1 - 1)
}
if tails[index].0 > tails[index - 1].0 && tails[index].1 < tails[index - 1].1 {
tails[index] = (tails[index].0 - 1 , tails[index].1 + 1)
}
}
}
var commands = inputStrings.compactMap({ Command($0) })
// part one
var count = 2
var tailPositions = Set<[Int]>()
var tails: [(Int, Int)] = Array(repeating: (0, 0), count: count)
commands.forEach({ executeCommand($0) })
print(tailPositions.count)
// part two
count = 10
tailPositions = Set<[Int]>()
tails = Array(repeating: (0, 0), count: count)
commands.forEach({ executeCommand($0) })
print(tailPositions.count)