-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13TransparentOrigami.kt
57 lines (48 loc) · 2.16 KB
/
Day13TransparentOrigami.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
package adventofcode.year2021
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day13TransparentOrigami(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val paper by lazy {
val dots = input.split("\n\n").first().lines().map { it.split(",").first().toInt() to it.split(",").last().toInt() }.toSet()
val maxX = dots.maxOf { it.first }
val maxY = dots.maxOf { it.second }
(0..maxY).map { y -> (0..maxX).map { x -> dots.contains(x to y) } }
}
private val instructions by lazy {
input.split("\n\n").last().lines().map { it.split("=").first().last() to it.split("=").last().toInt() }
}
override fun partOne() =
paper
.fold(instructions.first())
.flatten()
.count { it }
override fun partTwo() =
generateSequence(0 to paper.fold(instructions.first())) { (previousFold, previousPaper) ->
if (previousFold + 1 < instructions.size) previousFold + 1 to previousPaper.fold(instructions[previousFold + 1]) else null
}
.last()
.second
.joinToString(
separator = "\n",
prefix = "\n",
postfix = "\n",
) { row -> row.joinToString("") { cell -> if (cell) "█" else " " } }
companion object {
private fun List<List<Boolean>>.fold(instruction: Pair<Char, Int>) =
when (instruction.first) {
'x' -> foldLeft(instruction.second)
'y' -> foldUp(instruction.second)
else -> throw IllegalArgumentException("${instruction.first} is not a valid fold direction")
}
private fun List<List<Boolean>>.foldLeft(along: Int) =
map { it.take(along) }
.zip(map { it.takeLast(along).reversed() })
.map { it.first.zip(it.second) }
.map { it.map { it.first || it.second } }
private fun List<List<Boolean>>.foldUp(along: Int) =
take(along)
.zip(takeLast(along).reversed())
.map { it.first.zip(it.second) }
.map { it.map { it.first || it.second } }
}
}