Skip to content

Commit

Permalink
2024 - Day 14 - printing Christmas tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 14, 2024
1 parent 459762d commit c17c892
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
12 changes: 12 additions & 0 deletions src/main/kotlin/no/rodland/advent/Cave.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ operator fun Cave.get(pos: Pos): Char {
return this[pos.y][pos.x]
}

fun Cave.getOrNull(pos: Pos): Char? = if (pos in this) this[pos] else null

fun fromMap(map: Map<Pos, Char>): Cave {
val maxX = map.keys.maxOf { it.x }
val maxY = map.keys.maxOf { it.y }
return Array(maxY) { y ->
CharArray(maxX) { x ->
map[Pos(x, y)] ?: '.'
}
}
}

@Suppress("UNUSED_PARAMETER")
fun Cave.print(minX: Int = 0, minY: Int = 0, maxX: Int = first().size, maxY: Int = size) {
forEach { ca ->
Expand Down
32 changes: 13 additions & 19 deletions src/main/kotlin/no/rodland/advent_2024/Day12.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package no.rodland.advent_2024

import no.rodland.advent.Day
import no.rodland.advent.Direction
import no.rodland.advent.Pos
import no.rodland.advent.*

// template generated: 12/12/2024
// Fredrik Rødland 2024

class Day12(val input: List<String>) : Day<Int, Int, Array<CharArray>> {
class Day12(val input: List<String>) : Day<Int, Int, Cave> {

private val grid = input.parse()
private val cave = input.parse()


override fun partOne(): Int {
Expand All @@ -30,14 +28,14 @@ class Day12(val input: List<String>) : Day<Int, Int, Array<CharArray>> {

// got help from: https://todd.ginsberg.com/post/advent-of-code/2024/day12/
private fun Pos.corners(): Int {
val c = grid[this]
val c = cave.getOrNull(this)
return listOf(Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST, Direction.NORTH)
.zipWithNext()
.map { (first, second) ->
listOf(
grid[next(first)],
grid[next(second)],
grid[next(first).next(second)]
cave.getOrNull(next(first)),
cave.getOrNull(next(second)),
cave.getOrNull(next(first).next(second))
)
}.count { (side1, side2, corner) ->
(c != side1 && c != side2) || (side1 == c && side2 == c && corner != c)
Expand All @@ -46,22 +44,22 @@ class Day12(val input: List<String>) : Day<Int, Int, Array<CharArray>> {

@Suppress("ConvertCallChainIntoSequence")
private fun findAllRegions(): List<Region> {
fun getRegion(grid: Grid, pos: Pos, visited: MutableSet<Pos>): Set<Pos> {
fun getRegion(grid: Cave, pos: Pos, visited: MutableSet<Pos>): Set<Pos> {
return (setOf(pos) + pos.neighbourCellsUDLR()
.filter { it !in visited }
.filter { it in this@Day12.grid }
.filter { this@Day12.grid[it] == this@Day12.grid[pos] }
.filter { it in cave }
.filter { cave[it] == cave[pos] }
.onEach { visited.add(it) }
.flatMap { getRegion(grid, it, visited) }
.toList()).toSet()
}

val visited = mutableSetOf<Pos>()
return grid.flatMapIndexed { y, row ->
return cave.flatMapIndexed { y, row ->
row.mapIndexed { x, c ->
val pos = Pos(x, y)
if (pos !in visited) {
Region(c, getRegion(grid, pos, visited))
Region(c, getRegion(cave, pos, visited))
} else {
null
}
Expand All @@ -70,11 +68,7 @@ class Day12(val input: List<String>) : Day<Int, Int, Array<CharArray>> {
}


operator fun Grid.contains(pos: Pos): Boolean = pos.x >= 0 && pos.x < this[0].size && pos.y >= 0 && pos.y < this.size

operator fun Grid.get(pos: Pos): Char? = if (pos in this) grid[pos.y][pos.x] else null

override fun List<String>.parse(): Grid = indices.map { y -> indices.map { x -> this[y][x] }.toCharArray() }.toTypedArray<CharArray>()
override fun List<String>.parse(): Cave = indices.map { y -> indices.map { x -> this[y][x] }.toCharArray() }.toTypedArray<CharArray>()

override val day = "12".toInt()

Expand Down
22 changes: 19 additions & 3 deletions src/main/kotlin/no/rodland/advent_2024/Day14.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package no.rodland.advent_2024

import no.rodland.advent.Day
import no.rodland.advent.Pos
import no.rodland.advent.fromMap
import no.rodland.advent.print
import product

// template generated: 14/12/2024
Expand All @@ -22,13 +24,27 @@ class Day14(val input: List<String>, val width: Int, val height: Int) : Day<Long

override fun partTwo(): Int {
// no overlaps?
val hei = (0..width * height).map { i ->
val iterations = (0..width * height).map { i ->
val overlaps = robots.map { it.move(i, width, height) }
.groupBy { it }
.count { (_, v) -> v.size > 1 }
i to overlaps
}.first { it.second == 0 }
return hei.first
}.first { it.second == 0 }.first
if (width > 100) { // only print for live
printChristmasTree(iterations)
}
return iterations
}

private fun printChristmasTree(iterations: Int) {
val tree = robots
.asSequence()
.map { it.move(iterations, width, height) }
.map { Pos(it.x - 19, it.y - 17) }
.filter { it.x < 34 && it.y < 36 }
.map { it to '*' }
.toList().toMap()
fromMap(tree).print()
}

data class Robot(val pos: Pos, val vel: Pos) {
Expand Down

0 comments on commit c17c892

Please sign in to comment.