1
0

Day20 part1

This commit is contained in:
Hubert Van De Walle 2020-12-20 12:01:10 +01:00
parent 779ff4c398
commit af4d17a4a5
2 changed files with 1808 additions and 0 deletions

View File

@ -0,0 +1,80 @@
package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Groups
import be.vandewalleh.aoc.utils.input.Input
import be.vandewalleh.aoc.utils.input.createDay
private typealias Tile = Array<CharArray>
@Day(20)
class Day20(@Groups val input: Input<List<List<String>>>) {
private val tiles = input.value
.map { it[0].let { it.substring(5 until it.indexOf(':')).toInt() } to it.drop(1) }
.map { (id, tile) -> id to tile.map { it.toCharArray() }.toTypedArray() }
private fun Tile.sides() = sequence {
val tile = this@sides
yield(tile[0])
yield(tile[tile.lastIndex])
yield(tile[0].reversedArray())
yield(tile[tile.lastIndex].reversedArray())
yield(tile.map { it[0] }.toCharArray())
yield(tile.map { it[tile.lastIndex] }.toCharArray())
yield(tile.map { it[0] }.asReversed().toCharArray())
yield(tile.map { it[tile.lastIndex] }.asReversed().toCharArray())
}
private fun sideMatches(a: Tile, b: Tile): Boolean {
val aSides = a.sides()
val bSides = b.sides().toList()
aSides.forEach { aSide ->
bSides.forEach { bSide ->
if (aSide contentEquals bSide) return true
}
}
return false
}
private fun Tile.prettyPrint() {
val str = buildString {
this@prettyPrint.forEach { line ->
append(line.joinToString(""))
appendLine()
}
}
print(str)
}
private fun combine(tiles: List<Tile>) = sequence {
for (i in 0 until tiles.lastIndex) {
val a = tiles[i]
for (j in i + 1 until tiles.size) {
val b = tiles[j]
yield(a to b)
}
}
}
fun part1() = combine(tiles.map { it.second })
.filter { (a, b) -> sideMatches(a, b) }
.map { it.toList() }
.flatten()
.groupBy { it.map { it.toList() } }
.values
.asSequence()
.filter { it.size == 2 }
.flatten()
.toSet()
.map { tile -> tiles.find { it.second === tile }!! }
.map { it.first.toLong() }
.reduce { acc, id -> acc * id }
}
fun main() {
with(createDay<Day20>(
)) {
println(part1())
}
}

File diff suppressed because it is too large Load Diff