generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.kt
133 lines (105 loc) · 3.35 KB
/
Day13.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
package year2021.`13`
import readInput
private data class Point(
val x: Int,
val y: Int
)
private fun extractPoints(input: List<String>): Set<Point> {
return input.takeWhile { it.isNotBlank() }
.map {
val (x, y) = it.split(",")
.map { it.toInt() }
Point(x, y)
}
.toSet()
}
private sealed class Command {
data class FoldUp(val y: Int) : Command()
data class FoldLeft(val x: Int) : Command()
}
private fun extractCommands(input: List<String>): List<Command> {
return input.reversed()
.takeWhile { it.isNotBlank() }
.reversed()
.map {
val (coordindate, number) = it.split(" ").last()
.split("=")
when (coordindate) {
"x" -> Command.FoldLeft(x = number.toInt())
"y" -> Command.FoldUp(y = number.toInt())
else -> error("Illegal parsed data coordinate:$coordindate number:$number")
}
}
}
private fun foldUp(points: Set<Point>, command: Command.FoldUp): Set<Point> {
val y = command.y
val (below, above) = points
.partition { it.y > y }
.let { (below, above) -> below.toSet() to above.toSet() }
val resultSet = above.toMutableSet()
below.forEach {
val verticalDivide = it.y - y
resultSet.add(it.copy(y = y - verticalDivide))
}
return resultSet
}
private fun foldLeft(points: Set<Point>, command: Command.FoldLeft): Set<Point> {
val x = command.x
val (below, above) = points
.partition { it.x > x }
.let { (below, above) -> below.toSet() to above.toSet() }
val resultSet = above.toMutableSet()
below.forEach {
val horizontalDivide = it.x - x
resultSet.add(it.copy(x = x - horizontalDivide))
}
return resultSet
}
private fun fold(points: Set<Point>, command: Command): Set<Point> {
return when (command) {
is Command.FoldUp -> foldUp(points, command)
is Command.FoldLeft -> foldLeft(points, command)
}
}
private fun Collection<Point>.height(): Long = maxOf { it.y }.toLong()
private fun Collection<Point>.width(): Long = maxOf { it.x }.toLong()
private fun printSet(set: Set<Point>) {
val height = set.height()
println()
for (i in 0..height) {
print("|")
repeat(set.width().toInt() + 1) {
val symbol = if (Point(it, i.toInt()) in set) "█" else "."
print(symbol)
}
print("|")
println()
}
println()
}
fun main() {
fun part1(input: List<String>): Int {
val points = extractPoints(input)
val commands = extractCommands(input)
var buff = points
buff = fold(buff, commands.first())
return buff.size
}
fun part2(input: List<String>): Int {
val points = extractPoints(input)
val commands = extractCommands(input)
var buff = points
commands.forEach {
buff = fold(buff, it)
}
printSet(buff)
return buff.size
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day13_test")
val part1Test = part1(testInput)
println(part1Test.also { assert(it == 17) })
val input = readInput("Day13")
println(part1(input).also { assert(it == 710) })
println(part2(input).also { assert(it == 97) })
}