1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
dac50700b6 Day20 part2 WIP 2020-12-20 13:26:08 +01:00
af4d17a4a5 Day20 part1 2020-12-20 12:01:10 +01:00
2 changed files with 1983 additions and 0 deletions

View File

@ -0,0 +1,255 @@
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
import kotlin.math.sqrt
private typealias Tile = Array<CharArray>
private typealias Grid = Array<Array<Tile>>
@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)
}
}
}
private fun neighbours(): Map<Int, List<Tile>> {
val neighbours = combine(tiles.map { it.second })
.filter { (a, b) -> sideMatches(a, b) }
.map { it.toList() }
.flatten()
.groupBy { it.map { it.toList() } }
.values
val map = mutableMapOf<Int, MutableList<Tile>>()
for (neighbour in neighbours) {
map.computeIfAbsent(neighbour.size) { mutableListOf() }.add(neighbour.first())
}
return map
}
fun part1() = neighbours()[2]!!
.map { tile -> tiles.find { it.second === tile }!! }
.map { it.first.toLong() }
.reduce { acc, id -> acc * id }
operator fun Array<Array<Tile?>>.get(x: Int, y: Int): Tile? {
if (y < 0 || y > lastIndex) return null
val row = get(y)
return if (x < 0 || x > row.lastIndex) null
else row[x]
}
operator fun Array<Array<Tile?>>.set(x: Int, y: Int, tile: Tile) {
this[y][x] = tile
}
private fun fillGrid(): Grid {
val neighboursSet = neighbours().entries.associate { it.key to it.value.toMutableSet() }
val size = sqrt(tiles.size.toDouble()).toInt()
val grid: Array<Array<Tile?>> = Array(size) { Array(size) { null } }
for (x in 0 until size) {
for (y in 0 until size) {
var neighboursCount = 2
if (x != 0 && x != size - 1) neighboursCount++
if (y != 0 && y != size - 1) neighboursCount++
val pos = listOf(
x to y - 1,
x + 1 to y,
x to y + 1,
x - 1 to y,
)
val a = pos.mapNotNull { (x, y) -> grid[x, y] }
val iterator = neighboursSet[neighboursCount]!!.iterator()
while (iterator.hasNext()) {
val possibility = iterator.next()
val matches = a.count { sideMatches(it, possibility) }
if (matches == a.size) {
grid[x, y] = possibility
iterator.remove()
break
}
}
}
}
return grid as Grid
}
private fun correctGrid(grid: Grid): Grid {
TODO("flip / rotate every tile")
}
fun part2() {
val grid = correctGrid(fillGrid())
TODO("remove tile edges")
}
}
fun main() {
with(createDay<Day20>(
"""
Tile 2311:
..##.#..#.
##..#.....
#...##..#.
####.#...#
##.##.###.
##...#.###
.#.#.#..##
..#....#..
###...#.#.
..###..###
Tile 1951:
#.##...##.
#.####...#
.....#..##
#...######
.##.#....#
.###.#####
###.##.##.
.###....#.
..#.#..#.#
#...##.#..
Tile 1171:
####...##.
#..##.#..#
##.#..#.#.
.###.####.
..###.####
.##....##.
.#...####.
#.##.####.
####..#...
.....##...
Tile 1427:
###.##.#..
.#..#.##..
.#.##.#..#
#.#.#.##.#
....#...##
...##..##.
...#.#####
.#.####.#.
..#..###.#
..##.#..#.
Tile 1489:
##.#.#....
..##...#..
.##..##...
..#...#...
#####...#.
#..#.#.#.#
...#.#.#..
##.#...##.
..##.##.##
###.##.#..
Tile 2473:
#....####.
#..#.##...
#.##..#...
######.#.#
.#...#.#.#
.#########
.###.#..#.
########.#
##...##.#.
..###.#.#.
Tile 2971:
..#.#....#
#...###...
#.#.###...
##.##..#..
.#####..##
.#..####.#
#..#.#..#.
..####.###
..#.#.###.
...#.#.#.#
Tile 2729:
...#.#.#.#
####.#....
..#.#.....
....#..#.#
.##..##.#.
.#.####...
####.#.#..
##.####...
##..#.##..
#.##...##.
Tile 3079:
#.#.#####.
.#..######
..#.......
######....
####.#..#.
.#...#.##.
#.#####.##
..#.###...
..#.......
..#.###...
""".trimIndent()
)) {
println(part2())
}
}

File diff suppressed because it is too large Load Diff