Day 8, pretty cool
This commit is contained in:
55
src/main/kotlin/com/basdado/adventofcode/day8/Day8.kt
Normal file
55
src/main/kotlin/com/basdado/adventofcode/day8/Day8.kt
Normal 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
1
src/main/resources/day/8/input.txt
Normal file
1
src/main/resources/day/8/input.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user