1
0

Day20 part2 WIP

This commit is contained in:
Hubert Van De Walle 2020-12-20 13:26:08 +01:00
parent af4d17a4a5
commit dac50700b6

View File

@ -4,8 +4,10 @@ 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>>>) {
@ -57,24 +59,197 @@ class Day20(@Groups val input: Input<List<List<String>>>) {
}
}
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()
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(part1())
println(part2())
}
}