Skip to content

Commit

Permalink
Solution for 2023-12-21, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoziol committed Dec 21, 2023
1 parent 56d5b30 commit 62d94aa
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/kotlin/biz/koziolek/adventofcode/year2023/day21/day21.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package biz.koziolek.adventofcode.year2023.day21

import biz.koziolek.adventofcode.Coord
import biz.koziolek.adventofcode.findInput
import biz.koziolek.adventofcode.getAdjacentCoords
import biz.koziolek.adventofcode.parse2DMap

fun main() {
val inputFile = findInput(object {})
val map = parseGardenMap(inputFile.bufferedReader().readLines())
val steps = 64
println("${walk(map, steps).size} plots can be reached in $steps steps")
}

const val START = 'S'
const val GARDEN_PLOT = '.'
const val ROCK = '#'
const val REACHED_POS = 'O'

fun parseGardenMap(lines: Iterable<String>): Map<Coord, Char> =
lines.parse2DMap().toMap()

fun findStart(map: Map<Coord, Char>) =
map.entries.single { it.value == START }.key

fun walk(map: Map<Coord, Char>, steps: Int): Set<Coord> =
(1..steps).fold(setOf(findStart(map))) { positions, _ ->
positions
.flatMap { map.getAdjacentCoords(it, includeDiagonal = false) }
.filter { map[it] != ROCK }
.toSet()
}
131 changes: 131 additions & 0 deletions src/main/resources/biz/koziolek/adventofcode/year2023/day21/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
...................................................................................................................................
.......#.#.#.#.....#.#...##..........#........##....#......#...............##........#..#...##.......###....#.##........#.#....#...
.###............#........#......#.....#...................................#............#..........#.....#..##.#.......#.....#......
.......#....#...##......#..............##.###....##......#..................#.....#..#.....#...#.#....#................#....#.##...
..............#................#..#.#...#......#.#..#....................##....#..#.#.....#.#..........#...#.#......#...#..#.......
.....#.....#............##.#.....#......#..#...................................#......##..#.#..........#.#..##..#................#.
.#....#......##.#...#..#.....#................##.#....#.............................##...#...#........#....#.......................
....#.....#................#.....##...............#...............#...........#..#...........#.........#.............#........#....
.........#.................#...........#......#...#...............#...........#....#....#..........#...#.#.##.................#....
.........#....#..#.....#..#..#.#....#...#......................#................####...#.....#..........#.......#..#........#......
..............#.##...............#.#.#..........#..#............#.........................................#.#.#............#.#.....
..............#...#.....#...#....##.............##............##..#.................###........#....#.#.......................#..#.
......#.....##..#....#.#......#.#....#..#.#.....#............................................##.#.....#.#.......#......#.#.#.......
........#.............#.............#.....#................#.......#.....................#...#.........#..#...##..##...##..###.....
.......#..........................#........................................#........###..##......#.#.....#....#....#....#.......#..
...#......#.#..........#..#..........#..#.....#..........#........#.......................#....#.......#...##.............#.#......
..#..##...#...........#.#.#.##........##..#...................#....##..##..##...............#....#...........#......##..#.......#..
....................#.#...#.........##....................#.....#.....#..#....................................#.#.#........#.#.....
............##.#..#.#..#............#..#..#...........#.........#...#...............................#...............#...#........#.
.......##......#..............###......#............#.....#..........#..#.#.....#............#.#.......#..#...............#.....#..
.......###.......#......#..#..#...#...#...........#......#.........#...........#...................#.....##..........#....#........
..#...#.#..................##........................#..............##.....#.#.......................#...#..#...#..................
.........#.......#...#...#.....#......#..............##.......##....#.............##..............##.....#....................#....
..#....#.....#.....#............................#.#.......#.............#.....................#.........#.......#....##.#..........
...#.#...#........#....#........#.#...............#..##......#........#.........#...................#...#.......#...#......#...#...
.......................#..#..#.....#............#.#...#..#...........#.......##....#.#................#...................#....#...
.........#....#.#..........#.#....#..................#..............#...#...#..........#..........#.............##..#.......#..#...
...#......#.#......#.....#.##.#...#.....................#....##............#......#...#.........##...............#...............#.
........#.......#...................................#....#...............#....#.##..#............#...#........................#....
.......................#.......#..............#........#.#............#............................#...#......#..#..#.##.#.........
......#...#.##.............................##.#...................#..#......#.#...#...##..#.........##......#..#....#........#..#..
.........#.#............#................#...#.##......#..#.....#.##....##.#...........#....................#.............#........
.....#...#.........#...#.#...#.........#...#....#......#......##...#..#........#.....#.....#.............##.........#..........#...
.....#.......#............#.#............###..#..#.#.....#.##.......#.#.............##..##.....................#.#.......#..##.....
.#...#.....#...#.......................#...#.##...#....#........#.....#...#..................#............##..#...#....#.##.#...#..
......##.............#...#.........#.....#.....#..#........#......#..#......#..##...#..##.....#.#.................#..#.........#...
.........#..........#.#............###.................#..#.....#.....#..#...#..........#........#..........#.................#....
..#..........#....................#.....#..#...#....#.....#..#............#....#..#.........##..##..........#....#......#..........
.........#..........#..#.......#............#...........#.......#..#...........#.......##.....................#.......#............
...#...........................#..#.........#.#.....#.........#..........#........##.......#...#...............#....##.#.#....#....
....##.....#...#.......................#..#...#......#....#.#.....#..##.......#.........##.....................................#...
.#......#....##...#............#.....#..#........#......#...##.......#...#...#.#...#.#.....##..........................#..#........
.#.......##...................#..#......#..#..................##....#..#....#..##.......#..#.....###................#.#............
..............#.............#..#....#.....#.......#..#....##.........#..#.#.....###.......#.....##...##..........#..........#......
..#.#..#.................#......#...#.#...#......#...#.............#...#..##...............#........#............##.#.....#........
........#.....#.................#.....##.....#.#...#..#...........#..............#...#........#........##..............#...........
.#......#........................#..##.#........##......##....#...#........#..#...#..##.........#....#...............#.............
....#.....##..#............##.........#...#......#.#.....#.........#.#.#....###......................#...#.........................
.........###............##..........#.#.#.....#.....#..###.....................##.....#.....#....#.#...............................
...#.#.#............#..#.........###.....####.......#.....#...................#..##..##.......#....................................
.#.#.....#.#...............................#..#.......##........................#...#....#...##.#...#....................##........
...#.....................#...#...............#...#..#...#....#......##....#.#..#.#......#...#........#.....##.................##...
.........#.........#..#...#.#.#...#...#..........#......####.#.......#................#.........###...#..#.........................
....##..........#.........#..................#....#....#........#...#.........#..##...#....##.....#.#..........#..#..........#.....
...#............#...#..#.#.#.....#..##.#..#.......................#..#.....#....#...#.....#.....#..........#...#...#...............
.#..................................#..#..#..............#...#.....#...#................#.....#...#...........##..#................
....##................#....#.......#..#....#....#............##..........##...#.......#.......#...........#....#...................
....#..........#..........#..##......#.......###...........#............#..........#...#.#.#........#.#.#.#.#.#......#..........#..
............#...........................................#.......#.##.#.....................#..#..#..#.............#..............#.
...........#...#.........#.#....#................#.....#........#..#...#.#.....#..........#....................#...#..#............
...........#.#...#....#.....#..#..........#...#.#..#..#.#.........#.#.##........#......#................#...........#..............
................................................#..#..#....#.........#.............#....###..#...............#...#.......#.........
............................#...#..#......##......#...#........#....#.....#.....#...#..#...#..........................###.#........
..............#............#...#.#...##.......##.....#.##..........#....#............#.......#......##.#....#.......#..............
...........#.##.##..#.#.#.........###........#...#...#................#.#...............#..#.#........##........#.##.......#.......
.................................................................S.................................................................
.............#.#......#..##..#.............#...........#..........#..##.#...............#..###..............#...#.##.##....#.......
......#......##..#...............#..#............##.......#.#.......##..#...#.....##........#.................#....#......#........
..........#..........#.#........#....#......#........#.....#.............#....#..............#.#..#...#.............#..............
...........#........##.#.......#...#....#........#.#.....#........#..#..........#......##.....#...............#....#....#..........
...........#............#.....#..#.............#..#....#.#.....##............#.......#.......#............#.#..##..................
...........##..#...#.....##.........##.......#.......#.............#...#......#...##........#..#....#.......##..........#..........
.............#.......#......#..#...##..#.................................####...#......#.............#..#..#.......#...............
................#............#........#...##......##...................#..#...#.........#..#.........................#.............
.........................#...##.#......#...........#........#........#...............###....#.#........#............#..............
...#...........##........#..##.....#.....#.##......#....#..#........#.................#.#.........#........##..#..#................
..#.............#.##....#......#...#.#.......#.......#.................#.....#.....#.............###......#...#............##......
........................#.....#..##.........#.....#.....#..#........#...#.....#.....#....#......#...##..#...#.............#..#.....
......#........................#.....#..#...#....#......#....#..#.#.#...#.....#..#......#..#..#...#...#..#..#...............#......
..........#...........#......##.#.#............#...................##.....#..#...##....#..........#........#.............#.........
.#.......................#.....#..........#..##....#....#...............##......#.#..#.###..............................#..#..#....
.......................#........##......#.#......#......####..........#..#........#.#...........#..#.#..#.....#....................
....##.....................#..#.......###.................##.......#...#..#.###..#...#..###........#....#.#.............##....#....
........#..#............#...#.........#........#....#.##.......#...#.......#..#.#.......................................###........
.......##.....#..............#..#....#..#...............#......#................#................##......#.............#.#.........
....#......##...........###.....##................#...#....##.#......#....................#...##.#.......#............###...#......
.#...#.........#..........#..#.##.#.#...........#.#.........##....#.#......#........#.....#..#..........##.............#...#....##.
.........#.#.#....#..........#.....#...#..#.#...............#..........#....................##........................#.##....#..#.
........###..#..................#...#.....#.................#.#....#........#................#...#...........................#...#.
.....#.#..#........#.............#.........#.............#..###......#..#.#....#................##.....................##..........
.#......###....#....#........#.#..#...#.#..#.#...#........................#.........#...#.........#..........#.......#.........#...
..#.....#...........###........#.............#......#......#..........##.......#.#.#....#...#.................#...........#........
.....#........#..#..#................#......#....#.........#.##.......#...........#...........#.#............#.#....#......##......
.....#...##.#.....................#......................................#.#..#.#.....#.......#.#............####.#................
.......#...#......................####..#..#.............#..#.................#.....#...........#............#..#..........#.......
.##.............#...#.#...........................#..........##............#.......#.#......#............#........#................
.......#.......#.......#................#......#.#.#...#.............#........#........#.....#.#..............#....................
.#....#......#.#....#.................#.................##..............................#...............#.....###....#......#....#.
........##...............................#........#.............................#.....#..####.............#...#..#.........##.##...
.#.#............#..#..........#.......#......##.#.#..........#.........##.....#...........#..................#......##.......#.....
.................##.....#.#..............#.#.#.###...#.............#.#....#.....##...#.##..#...........#.#.#....#..................
..#.#..#..#................................#.#.......##.............#.#..........###................#.....##....#..................
........#........#.##..#.....##.#........#.#...#..#..#.....#...#....#.#.......#...#.....#..............#.........#.#...#...........
........#.....##..#..#......................#................#...............#...#.#.#..........#.#.##......................#......
........#......#.....#.#.......#............#...........##...............#............#...........#..........#.#..........#.#......
......###.....#.................###................#......#........#...#.#.#...#....#.#.........#...#...##........#...#............
..#.##..#..#....#.....##..........#............#................#.#..#.........................#.##..##..........##....#...#.....#.
....#............#.#..#...#......#..............#..#....##...#..#.....#.........#...#...........#..........#.#......#.#...##.###...
....#................#.........#...............##..#...........#.......#.#..#.......................#....#...##........#...........
....#....................##......#...#...........#.......#.#..#..............##.#....................#....#...................#....
...#..............##......#..............#...........#.......#......#........##.............#.........#...#.#..##..................
....#........#.#....#....#.#.....#........#..........................#..#.#.#.#.........#..........#.##...#.##..###.....#..........
.#.......#.#............#..#....#.......#.#............#..........#.#........##.............#...#..#....#..........#..........#....
...#...#......#..............#...##.......#..........#.....#......##...#..............#................#..............#...##..##...
....#......#.##..##....#.....................#..........#....##......#................#.......#.#..................#......#.#......
...#...............#..#..........#..#.....#..##............#.......###...........................##................#...#...........
........#....#........#.......#....#..#.#..................#.............##.............#..#.......##..........................#...
.#....................#......#.#........#..................#.##.#..#.....#.........##................................#.............
.......#....................#..#................#......................................##..........#.....#..##..#..........#.#..#..
..#.....#..#....#.....#......#..##...#.........#.........................................#...#....#.........#...##.......#.#.....#.
...........#..........#..#.#...#...##..#.........#.#...........#....#..............#.............#.......##..#...#....##.......#...
..#.........#.#..#...#.....#......................#..........#........#.......#....................#...##......#.................#.
.#......#...#.#.#......#.#........#.#......#....#....#............................#......####............#.......#..#..........##..
.#......##..........#...#...........#....#...#.#.................................#.##...#......................#..#..........#.#...
.....#...#.......#...................##.#....#...##............#..##.........#..................#..#......#..........#....#.....##.
.#..................#..........#.....................#.#.........................#...........#...##...#...#...##.#.................
.#..#..#..#..#......#........#........#.......#.#...#.#..................#.......#.......#..#.#......#...#...............#.....#...
.#........#......................#...##.......#..##......................###......#.....##....#..#................#................
.#...........#.............#..#.....##.........#.##........#.............#....#.#...........#...............#.........#........#...
......#....#.#...###................#....#......#..#........#.........................##.##...#..#..#.#......#.....................
...................................................................................................................................
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package biz.koziolek.adventofcode.year2023.day21

import biz.koziolek.adventofcode.findInput
import biz.koziolek.adventofcode.getHeight
import biz.koziolek.adventofcode.getWidth
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test

@Tag("2023")
internal class Day21Test {

private val sampleInput = """
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
""".trimIndent().split("\n")

@Test
fun testParse() {
val map = parseGardenMap(sampleInput)
assertEquals(11, map.getWidth())
assertEquals(11, map.getHeight())
assertEquals(40, map.count { it.value == ROCK })
}

@Test
fun testSampleAnswer1() {
val map = parseGardenMap(sampleInput)
assertEquals(2, walk(map, steps = 1).size)
assertEquals(4, walk(map, steps = 2).size)
assertEquals(6, walk(map, steps = 3).size)
assertEquals(16, walk(map, steps = 6).size)
}

@Test
@Tag("answer")
fun testAnswer1() {
val input = findInput(object {}).bufferedReader().readLines()
val map = parseGardenMap(input)
assertEquals(3594, walk(map, steps = 64).size)
}
}

0 comments on commit 62d94aa

Please sign in to comment.