Skip to content

Commit

Permalink
2023 - Day 21 - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 21, 2023
1 parent 2e18735 commit 0aaf270
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
33 changes: 26 additions & 7 deletions src/main/kotlin/no/rodland/advent_2023/Day21.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
package no.rodland.advent_2023

import no.rodland.advent.Day
import no.rodland.advent.Pos


// template generated: 21/12/2023
// Fredrik Rødland 2023

class Day21(val input: List<String>) : Day<Long, Long, List<String>> {
class Day21(val input: List<String>) : Day<Int, Long, Map<Pos, Char>> {

private val parsed = input.parse()

override fun partOne(): Long {
return 2
override fun partOne(): Int {
// val start = parsed.filterValues { it == 'S' }.map { it.key }.single()
var newMap = parsed
val repeat = if (parsed.size == 121) 6 else 64
repeat(repeat) { newMap = step(newMap) }
return newMap.count { it.value == 'O' }
}

fun step(cave: Map<Pos, Char>): Map<Pos, Char> {
val previousSteps = cave.filterValues { it == 'O' || it == 'S' }.keys
val newOs = previousSteps
.flatMap { it.neighbourCellsUDLR() }
.filterNot { cave[it] == '#' }
.map { it to 'O' }
val nullOld = previousSteps.map { it to '.' }
return cave + nullOld + newOs
}


override fun partTwo(): Long {
return 2
}

override fun List<String>.parse(): List<String> {
return map { line ->
line
}
override fun List<String>.parse(): Map<Pos, Char> {
return flatMapIndexed { y, line ->
line.mapIndexed() { x, c ->
Pos(x, y) to c
}
}.toMap()
}

override val day = "21".toInt()
Expand Down
5 changes: 3 additions & 2 deletions src/test/kotlin/no/rodland/advent_2023/Day21Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ internal class Day21Test {
private val data21 = "2023/input_21.txt".readFile()
private val test21 = "2023/input_21_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestOne = 16
private val resultTestTwo = 2L
private val resultOne = 2L
private val resultOne = 3814
private val resultTwo = 2L

val test = defaultTestSuiteParseOnInit(
Expand All @@ -29,6 +29,7 @@ internal class Day21Test {
resultTwo,
{ Day21(data21) },
{ Day21(test21) },
numTestPart1 = 2
)

@Nested
Expand Down

0 comments on commit 0aaf270

Please sign in to comment.