-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.kt
96 lines (85 loc) · 2.61 KB
/
Main.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
import java.io.File
fun main() {
val points = arrayListOf<Pair<Int, Int>>()
val folds = arrayListOf<String>()
File("src/input")
.readLines()
.forEach {
if (it.contains("fold")) folds.add(it)
else if (it.isNotEmpty()) {
val point = it.split(",")
points.add(Pair(point[0].toInt(), point[1].toInt()))
}
}
val matrix = createMat(points)
println("Part 1: " + solve(matrix, folds, true))
println("Part 2:")
solve(matrix, folds, false)
}
private fun createMat(points: ArrayList<Pair<Int, Int>>): Array<CharArray> {
val maxX = points.maxByOrNull { it.second }!!.second + 1
val maxY = points.maxByOrNull { it.first }!!.first + 1
val matrix = Array(maxX) { CharArray(maxY) { '.' } }
points.forEach { matrix[it.second][it.first] = '#' }
return matrix
}
fun solve(matrix: Array<CharArray>, folds: ArrayList<String>, stopEarly: Boolean): Int {
var rowSize = matrix.size
var colSize = matrix[0].size
for (fold in folds) {
if (fold.contains("fold along y=")) {
rowSize = foldY(fold, matrix)
} else if (fold.contains("fold along x=")) {
colSize = foldX(fold, matrix)
}
if (stopEarly)
return matrix.countChar('#')
}
printMat(matrix, rowSize, colSize)
return matrix.countChar('#')
}
private fun foldX(fold: String, matrix: Array<CharArray>): Int {
val maxX = fold.split("fold along x=")[1].toInt()
for (i in matrix.indices) {
for (j in matrix[0].indices) {
if (j < maxX) {
continue
}
val newJ = maxX - (j - maxX)
if (matrix[i][j] != '.') matrix[i][newJ] = matrix[i][j]
matrix[i][j] = '.'
}
}
return maxX
}
private fun foldY(fold: String, matrix: Array<CharArray>): Int {
val maxY = fold.split("fold along y=")[1].toInt()
for (i in matrix.indices) {
if (i < maxY) {
continue
}
for (j in matrix[0].indices) {
val newI = maxY - (i - maxY)
if (matrix[i][j] != '.') matrix[newI][j] = matrix[i][j]
matrix[i][j] = '.'
}
}
return maxY
}
fun Array<CharArray>.countChar(character: Char): Int {
var counter = 0
map {
it.map {
if (it == character) counter++
}
}
return counter
}
private fun printMat(matrix: Array<CharArray>, rowSize: Int, colSize: Int) {
for (row in 0 until rowSize) {
for (col in 0 until colSize) {
print("${matrix[row][col]} ")
}
println()
}
}