From 58b69bb549dc67a11fd8b2e67b111ececbfda392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20R=C3=B8dland?= Date: Tue, 10 Dec 2024 09:43:27 +0100 Subject: [PATCH] 2024 - Day 10 - part 1 --- .../kotlin/no/rodland/advent_2024/Day10.kt | 43 +++++++++++++++---- .../no/rodland/advent_2024/Day10Test.kt | 5 ++- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/no/rodland/advent_2024/Day10.kt b/src/main/kotlin/no/rodland/advent_2024/Day10.kt index 21ef9ff..e7d2c37 100644 --- a/src/main/kotlin/no/rodland/advent_2024/Day10.kt +++ b/src/main/kotlin/no/rodland/advent_2024/Day10.kt @@ -1,27 +1,54 @@ package no.rodland.advent_2024 import no.rodland.advent.Day +import no.rodland.advent.Pos // template generated: 10/12/2024 // Fredrik Rødland 2024 - -class Day10(val input: List) : Day> { +class Day10(val input: List) : Day, Set>, Array>> { private val parsed = input.parse() + private val heads = parsed.first.first + private val tails = parsed.first.second + private val grid = parsed.second - override fun partOne(): Long { - return 2 + override fun partOne(): Int { + return heads.sumOf { it.score(mutableSetOf()) } } override fun partTwo(): Long { return 2 } - override fun List.parse(): List { - return map { line -> - line - } + private fun Pos.score(visited: MutableSet): Int { + visited.add(this) + val c = grid[this].digitToInt() + return if (c == 9) return 1 + else neighbourCellsUDLR() + .filterNot { it in visited } + .filter { it in grid } + .filter { grid[it].digitToInt() == c + 1 } + .sumOf { it.score(visited) } + } + + 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 = this[pos.y][pos.x] + + override fun List.parse(): Pair, Set>, Array> { + val heads = mutableSetOf() + val tails = mutableSetOf() + val grid = indices.map { y -> + indices.map { x -> + val c = this[y][x] + if (c == '0') heads.add(Pos(x, y)) + if (c == '9') tails.add(Pos(x, y)) + c + }.toCharArray() + }.toTypedArray() + return (heads to tails) to grid } override val day = "10".toInt() } + diff --git a/src/test/kotlin/no/rodland/advent_2024/Day10Test.kt b/src/test/kotlin/no/rodland/advent_2024/Day10Test.kt index 4056334..f79f942 100644 --- a/src/test/kotlin/no/rodland/advent_2024/Day10Test.kt +++ b/src/test/kotlin/no/rodland/advent_2024/Day10Test.kt @@ -15,9 +15,9 @@ internal class Day10Test { private val data10 = "2024/input_10.txt".readFile() private val test10 = "2024/input_10_test.txt".readFile() - private val resultTestOne = 2L + private val resultTestOne = 36 private val resultTestTwo = 2L - private val resultOne = 2L + private val resultOne = 514 private val resultTwo = 2L val test = defaultTestSuiteParseOnInit( @@ -29,6 +29,7 @@ internal class Day10Test { resultTwo, { Day10(data10) }, { Day10(test10) }, + numTestPart1 = 200 ) @Nested