Day 8, pretty cool

This commit is contained in:
2019-12-09 01:15:47 +01:00
parent 579b97e2e6
commit 46e7df3308
2 changed files with 56 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
package com.basdado.adventofcode.day8
import com.basdado.adventofcode.line
import java.util.concurrent.atomic.AtomicInteger
import java.util.stream.Collectors
const val DAY8_INPUT_PATH = "/day/8/input.txt"
const val IMG_WIDTH = 25
const val IMG_HEIGHT = 6
fun main() {
Day8.puzzle1()
Day8.puzzle2()
}
object Day8 {
fun puzzle1() {
val counter = AtomicInteger()
val layer = line(DAY8_INPUT_PATH)
.chars()
.map { it - '0'.toInt() }
.mapToObj { it }
.collect(Collectors.groupingBy<Int, Int> { counter.getAndIncrement() / (IMG_WIDTH * IMG_HEIGHT) })
.minBy { it.value.stream().filter { n -> n == 0 }.count() }!!
.value
println(layer.count { it == 1 } * layer.count { it == 2 })
}
fun puzzle2() {
val counter = AtomicInteger()
val img = line(DAY8_INPUT_PATH)
.chars()
.map { it - '0'.toInt() }
.mapToObj { it }
.collect(Collectors.groupingBy<Int, Int> { counter.getAndIncrement() % (IMG_WIDTH * IMG_HEIGHT) })
.map { it.value.firstOrNull { n -> n != 2 } }
for(i in 0 until IMG_HEIGHT) {
for (j in 0 until IMG_WIDTH) {
print(if(img[i * IMG_WIDTH + j] == 1) "#" else " ")
}
println()
}
}
}

File diff suppressed because one or more lines are too long