-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay08.kt
59 lines (55 loc) · 1.87 KB
/
Day08.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
import java.io.File
import java.lang.IllegalArgumentException
import java.util.*
fun main() {
val vals =
File("files/day8.txt").bufferedReader().readLine().toCharArray().toList().map { x: Char -> x.toInt() - 48 }
.toList()
println(Day08().part1(vals, 25, 6))
Day08().part2(vals, 25, 6)
}
class Day08 {
fun part1(list: List<Int>, width: Int, height: Int): Int {
val layers: MutableList<MutableList<Int>> = mutableListOf()
for ((i, c) in list.withIndex()) {
if (i / (width * height) >= layers.size) {
layers.add(mutableListOf())
}
layers[i / (width * height)].add(c)
}
var zeroes = Integer.MAX_VALUE
var ones = 0
var twos = 0
for (l in layers) {
if (l.count { x -> x == 0 } < zeroes) {
zeroes = l.count { x -> x == 0 }
ones = l.count { x -> x == 1 }
twos = l.count { x -> x == 2 }
}
}
return ones * twos
}
fun part2(list: List<Int>, width: Int, height: Int) {
val layers: MutableList<MutableList<Int>> = mutableListOf()
for ((i, c) in list.withIndex()) {
if (i / (width * height) >= layers.size) {
layers.add(mutableListOf())
}
layers[i / (width * height)].add(c)
}
val imageMutable: MutableList<Int> = mutableListOf()
for (i in 0..width * height) {
imageMutable.add(2)
}
var image: List<Int> = imageMutable.toList()
for (l in layers.reversed()) {
image = image.zip(l).map { x -> if (x.second != 2) x.second else x.first }
}
for ((i, p) in image.map { x -> if (x == 1) "##" else " " }.withIndex()) {
if (i % width == 0) {
println()
}
print(p)
}
}
}