Compare commits
2 Commits
779ff4c398
...
dac50700b6
| Author | SHA1 | Date | |
|---|---|---|---|
| dac50700b6 | |||
| af4d17a4a5 |
255
days/src/main/kotlin/Day20.kt
Normal file
255
days/src/main/kotlin/Day20.kt
Normal 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())
|
||||||
|
}
|
||||||
|
}
|
||||||
1728
days/src/main/resources/day20.txt
Normal file
1728
days/src/main/resources/day20.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user