-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay13.kt
More file actions
65 lines (55 loc) 路 2.03 KB
/
Copy pathDay13.kt
File metadata and controls
65 lines (55 loc) 路 2.03 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
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
import java.io.File
fun main() {
part1("src/main/resources/day13sample.txt")
part1("src/main/resources/day13.txt")
part2("src/main/resources/day13sample.txt")
part2("src/main/resources/day13.txt")
}
//typealias Point = Pair<Int, Int> //already defined in day5
typealias PaperFold = Pair<Char, Int>
fun parsePoints(inputFile: String): Set<Point> =
File(inputFile).readLines()
.takeWhile { it.contains(",") }
.map { line -> line.split(",") }
.map { line -> line[0].toInt() to line[1].toInt() }
.toSet()
fun parseFolds(inputFile: String): List<PaperFold> =
File(inputFile).readLines()
.takeLastWhile { it.contains("fold") }
.map { line -> line.takeLastWhile { it != ' ' }.split("=") }
.map { line -> line[0][0] to line[1].toInt() }
fun Set<Point>.foldPaperBy(fold: PaperFold): Set<Point> =
when(fold.first) {
'x' -> {
this.filter { it.first > fold.second }
.map { fold.second-(it.first-fold.second) to it.second }
.toSet() union this.filter { it.first < fold.second }
}
'y' -> {
this.filter { it.second > fold.second }
.map { it.first to fold.second-(it.second-fold.second) }
.toSet() union this.filter { it.second < fold.second }
}
else -> error("invalid fold type")
}
fun Set<Point>.printOnGrid() {
val width = this.maxOf { it.first }
val height = this.maxOf { it.second }
(0 .. height).forEach { y ->
(0 .. width).forEach { x ->
if(this.contains(x to y)) print("#") else print(".")
}
println()
}
}
private fun part1(inputFile: String) {
val points = parsePoints(inputFile)
val folds = parseFolds(inputFile)
points.foldPaperBy(folds.first()).apply { println(this.size) }
}
private fun part2(inputFile: String) {
val points = parsePoints(inputFile)
val folds = parseFolds(inputFile)
folds.fold(points) { acc, f -> acc.foldPaperBy(f) }
.apply { this.printOnGrid() }
}