-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay13.kt
More file actions
39 lines (34 loc) · 1.53 KB
/
Day13.kt
File metadata and controls
39 lines (34 loc) · 1.53 KB
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
import kotlin.math.abs
fun main() {
fun solve(input: List<String>, part1: Boolean) {
val index = input.indexOf("")
val points = input.take(index).map { it.split(",").map(String::toInt) }
val folds = input.drop(index + 1).map { s -> s.split(" ").last().let { it[0] to it.drop(2).toInt() } }
var grid = points.map { it[0] to it[1] }.toSet()
folds.forEach { (dir, n) ->
grid = grid.map { (x, y) ->
if (dir == 'x') n - abs(x - n) to y
else x to n - abs(y - n)
}.toSet()
if (part1) {
println(grid.size)
return
}
}
val (x, y) = grid.maxOf { it.first } to grid.maxOf { it.second }
(0..y).forEach { j -> println((0..x).joinToString(" ") { i -> if (i to j in grid) "#" else " " }) }
}
val testInput = readInput("Day13_test")
solve(testInput, part1 = true)
solve(testInput, part1 = false)
val input = readInput("Day13")
solve(input, part1 = true)
solve(input, part1 = false)
}
// Output:
// # # # # # # # # # # # # # # # # # #
// # # # # # # # # # # # # #
// # # # # # # # # # # # # # #
// # # # # # # # # # # # # # # # #
// # # # # # # # # # # # # # # #
// # # # # # # # # # # # # # # # # # # #