generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay05.kt
190 lines (161 loc) · 4.31 KB
/
Day05.kt
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
package year2022.`05`
import java.util.Stack
import readInput
fun main() {
fun part1(input: List<String>): String {
val (mutableStacks, listOfCommands) = prepareStacksToCommands(input)
applyCommandsToStacks(
mutableStacks = mutableStacks,
commands = listOfCommands
)
return peekListOfStacks(mutableStacks).joinToString("")
}
fun part2(input: List<String>): String {
val (mutableStacks, listOfCommands) = prepareStacksToCommands(input)
applyCommandsToStacksPart2(
mutableStacks = mutableStacks,
commands = listOfCommands
)
return peekListOfStacks(mutableStacks).joinToString("")
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day05_test")
val part1Test = part1(testInput)
//println(part1Test)
check(part1Test == "CMZ")
val input = readInput("Day05")
println(part1(input))
println(part2(input))
}
/**
* Class for working with moving commands
*/
data class Command(
val amount: Int,
val from: Int,
val to: Int
)
/**
* Build list of stacks from input
*/
fun buildListOfStacks(
amountOfStacks: Int,
lines: List<List<String?>>
): List<Stack<String>> {
// Build initial empty stacks
val stacks = mutableListOf<Stack<String>>()
repeat(amountOfStacks) {
stacks.add(Stack<String>())
}
// Fill stacks with values
for (item in lines.reversed()) {
repeat(amountOfStacks) {
// Workaround for cases, when input line is not full
try {
val crate = item[it]
if (crate != null) {
stacks[it].add(crate)
}
} catch (e: Exception) {
return@repeat
}
}
}
return stacks
}
/**
* Parse string "[W] [W] [N] [L] [V] [W] [C] "
* to list of [W,W,null,N,L,V,W,C,null]
*/
fun cratesInputToLists(input: List<String>): List<List<String?>> {
return input.map {
it.windowed(3, 4)
.map { crateString ->
if (crateString.isBlank()) {
null
} else {
crateString[1].toString()
}
}
}
}
/**
* Parse list of stack amount and fetch max amount
*/
fun crateAmountToInt(amountLine: String): Int {
return amountLine.split(" ")
.last { it.isNotEmpty() }
.toInt()
}
/**
* Move "Crates" one by one
*/
fun applyCommandsToStacks(
mutableStacks: List<Stack<String>>,
commands: List<Command>
) {
commands.forEach { (amount, from, to) ->
val fromStack = mutableStacks[from - 1]
val toStack = mutableStacks[to - 1]
repeat(amount) {
val itemToMove = fromStack.pop()
toStack.push(itemToMove)
}
}
}
/**
* Move "Crates" all together
*/
fun applyCommandsToStacksPart2(
mutableStacks: List<Stack<String>>,
commands: List<Command>
) {
commands.forEach { (amount, from, to) ->
val fromStack = mutableStacks[from - 1]
val toStack = mutableStacks[to - 1]
val listToMove = mutableListOf<String>()
repeat(amount) {
listToMove.add(fromStack.pop())
}
toStack.addAll(listToMove.reversed())
}
}
/**
* Parse strings to list of [Command]
*/
fun buildListOfCommands(
lines: List<String>
): List<Command> {
return lines.map {
val keys = it.split(" ")
Command(keys[1].toInt(), keys[3].toInt(), keys[5].toInt())
}
}
/**
* Peek list of stacks and return top items
*/
fun peekListOfStacks(
stacks: List<Stack<String>>
): List<String> {
return stacks.map { it.peek() }
}
/**
* Parse all input to data
*/
fun prepareStacksToCommands(
input: List<String>
): Pair<List<Stack<String>>, List<Command>> {
// Part 1
val firstPart = input.takeWhile { it.isNotBlank() }
val crates = firstPart.dropLast(1)
val cratesAmountInput = firstPart.last()
val mutableStacks = buildListOfStacks(
amountOfStacks = crateAmountToInt(cratesAmountInput),
lines = cratesInputToLists(crates)
)
// Part 2
val listOfCommands = buildListOfCommands(
lines = input.reversed().takeWhile { it.isNotBlank() }.reversed()
)
return mutableStacks to listOfCommands
}