From dac50700b6e1a173654036acc53047090de71aca Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Sun, 20 Dec 2020 13:26:08 +0100 Subject: [PATCH] Day20 part2 WIP --- days/src/main/kotlin/Day20.kt | 197 ++++++++++++++++++++++++++++++++-- 1 file changed, 186 insertions(+), 11 deletions(-) diff --git a/days/src/main/kotlin/Day20.kt b/days/src/main/kotlin/Day20.kt index 694e411..e158fe7 100644 --- a/days/src/main/kotlin/Day20.kt +++ b/days/src/main/kotlin/Day20.kt @@ -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 +private typealias Grid = Array> @Day(20) class Day20(@Groups val input: Input>>) { @@ -57,24 +59,197 @@ class Day20(@Groups val input: Input>>) { } } - 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> { + 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>() + 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>.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>.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(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( + """ + Tile 2311: + ..##.#..#. + ##..#..... + #...##..#. + ####.#...# + ##.##.###. + ##...#.### + .#.#.#..## + ..#....#.. + ###...#.#. + ..###..### + + Tile 1951: + #.##...##. + #.####...# + .....#..## + #...###### + .##.#....# + .###.##### + ###.##.##. + .###....#. + ..#.#..#.# + #...##.#.. + + Tile 1171: + ####...##. + #..##.#..# + ##.#..#.#. + .###.####. + ..###.#### + .##....##. + .#...####. + #.##.####. + ####..#... + .....##... + + Tile 1427: + ###.##.#.. + .#..#.##.. + .#.##.#..# + #.#.#.##.# + ....#...## + ...##..##. + ...#.##### + .#.####.#. + ..#..###.# + ..##.#..#. + + Tile 1489: + ##.#.#.... + ..##...#.. + .##..##... + ..#...#... + #####...#. + #..#.#.#.# + ...#.#.#.. + ##.#...##. + ..##.##.## + ###.##.#.. + + Tile 2473: + #....####. + #..#.##... + #.##..#... + ######.#.# + .#...#.#.# + .######### + .###.#..#. + ########.# + ##...##.#. + ..###.#.#. + + Tile 2971: + ..#.#....# + #...###... + #.#.###... + ##.##..#.. + .#####..## + .#..####.# + #..#.#..#. + ..####.### + ..#.#.###. + ...#.#.#.# + + Tile 2729: + ...#.#.#.# + ####.#.... + ..#.#..... + ....#..#.# + .##..##.#. + .#.####... + ####.#.#.. + ##.####... + ##..#.##.. + #.##...##. + + Tile 3079: + #.#.#####. + .#..###### + ..#....... + ######.... + ####.#..#. + .#...#.##. + #.#####.## + ..#.###... + ..#....... + ..#.###... + """.trimIndent() )) { - println(part1()) + println(part2()) } }