-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/biz/koziolek/adventofcode/year2023/day21/day21.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
131
src/main/resources/biz/koziolek/adventofcode/year2023/day21/input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
................................................................................................................................... | ||
.......#.#.#.#.....#.#...##..........#........##....#......#...............##........#..#...##.......###....#.##........#.#....#... | ||
.###............#........#......#.....#...................................#............#..........#.....#..##.#.......#.....#...... | ||
.......#....#...##......#..............##.###....##......#..................#.....#..#.....#...#.#....#................#....#.##... | ||
..............#................#..#.#...#......#.#..#....................##....#..#.#.....#.#..........#...#.#......#...#..#....... | ||
.....#.....#............##.#.....#......#..#...................................#......##..#.#..........#.#..##..#................#. | ||
.#....#......##.#...#..#.....#................##.#....#.............................##...#...#........#....#....................... | ||
....#.....#................#.....##...............#...............#...........#..#...........#.........#.............#........#.... | ||
.........#.................#...........#......#...#...............#...........#....#....#..........#...#.#.##.................#.... | ||
.........#....#..#.....#..#..#.#....#...#......................#................####...#.....#..........#.......#..#........#...... | ||
..............#.##...............#.#.#..........#..#............#.........................................#.#.#............#.#..... | ||
..............#...#.....#...#....##.............##............##..#.................###........#....#.#.......................#..#. | ||
......#.....##..#....#.#......#.#....#..#.#.....#............................................##.#.....#.#.......#......#.#.#....... | ||
........#.............#.............#.....#................#.......#.....................#...#.........#..#...##..##...##..###..... | ||
.......#..........................#........................................#........###..##......#.#.....#....#....#....#.......#.. | ||
...#......#.#..........#..#..........#..#.....#..........#........#.......................#....#.......#...##.............#.#...... | ||
..#..##...#...........#.#.#.##........##..#...................#....##..##..##...............#....#...........#......##..#.......#.. | ||
....................#.#...#.........##....................#.....#.....#..#....................................#.#.#........#.#..... | ||
............##.#..#.#..#............#..#..#...........#.........#...#...............................#...............#...#........#. | ||
.......##......#..............###......#............#.....#..........#..#.#.....#............#.#.......#..#...............#.....#.. | ||
.......###.......#......#..#..#...#...#...........#......#.........#...........#...................#.....##..........#....#........ | ||
..#...#.#..................##........................#..............##.....#.#.......................#...#..#...#.................. | ||
.........#.......#...#...#.....#......#..............##.......##....#.............##..............##.....#....................#.... | ||
..#....#.....#.....#............................#.#.......#.............#.....................#.........#.......#....##.#.......... | ||
...#.#...#........#....#........#.#...............#..##......#........#.........#...................#...#.......#...#......#...#... | ||
.......................#..#..#.....#............#.#...#..#...........#.......##....#.#................#...................#....#... | ||
.........#....#.#..........#.#....#..................#..............#...#...#..........#..........#.............##..#.......#..#... | ||
...#......#.#......#.....#.##.#...#.....................#....##............#......#...#.........##...............#...............#. | ||
........#.......#...................................#....#...............#....#.##..#............#...#........................#.... | ||
.......................#.......#..............#........#.#............#............................#...#......#..#..#.##.#......... | ||
......#...#.##.............................##.#...................#..#......#.#...#...##..#.........##......#..#....#........#..#.. | ||
.........#.#............#................#...#.##......#..#.....#.##....##.#...........#....................#.............#........ | ||
.....#...#.........#...#.#...#.........#...#....#......#......##...#..#........#.....#.....#.............##.........#..........#... | ||
.....#.......#............#.#............###..#..#.#.....#.##.......#.#.............##..##.....................#.#.......#..##..... | ||
.#...#.....#...#.......................#...#.##...#....#........#.....#...#..................#............##..#...#....#.##.#...#.. | ||
......##.............#...#.........#.....#.....#..#........#......#..#......#..##...#..##.....#.#.................#..#.........#... | ||
.........#..........#.#............###.................#..#.....#.....#..#...#..........#........#..........#.................#.... | ||
..#..........#....................#.....#..#...#....#.....#..#............#....#..#.........##..##..........#....#......#.......... | ||
.........#..........#..#.......#............#...........#.......#..#...........#.......##.....................#.......#............ | ||
...#...........................#..#.........#.#.....#.........#..........#........##.......#...#...............#....##.#.#....#.... | ||
....##.....#...#.......................#..#...#......#....#.#.....#..##.......#.........##.....................................#... | ||
.#......#....##...#............#.....#..#........#......#...##.......#...#...#.#...#.#.....##..........................#..#........ | ||
.#.......##...................#..#......#..#..................##....#..#....#..##.......#..#.....###................#.#............ | ||
..............#.............#..#....#.....#.......#..#....##.........#..#.#.....###.......#.....##...##..........#..........#...... | ||
..#.#..#.................#......#...#.#...#......#...#.............#...#..##...............#........#............##.#.....#........ | ||
........#.....#.................#.....##.....#.#...#..#...........#..............#...#........#........##..............#........... | ||
.#......#........................#..##.#........##......##....#...#........#..#...#..##.........#....#...............#............. | ||
....#.....##..#............##.........#...#......#.#.....#.........#.#.#....###......................#...#......................... | ||
.........###............##..........#.#.#.....#.....#..###.....................##.....#.....#....#.#............................... | ||
...#.#.#............#..#.........###.....####.......#.....#...................#..##..##.......#.................................... | ||
.#.#.....#.#...............................#..#.......##........................#...#....#...##.#...#....................##........ | ||
...#.....................#...#...............#...#..#...#....#......##....#.#..#.#......#...#........#.....##.................##... | ||
.........#.........#..#...#.#.#...#...#..........#......####.#.......#................#.........###...#..#......................... | ||
....##..........#.........#..................#....#....#........#...#.........#..##...#....##.....#.#..........#..#..........#..... | ||
...#............#...#..#.#.#.....#..##.#..#.......................#..#.....#....#...#.....#.....#..........#...#...#............... | ||
.#..................................#..#..#..............#...#.....#...#................#.....#...#...........##..#................ | ||
....##................#....#.......#..#....#....#............##..........##...#.......#.......#...........#....#................... | ||
....#..........#..........#..##......#.......###...........#............#..........#...#.#.#........#.#.#.#.#.#......#..........#.. | ||
............#...........................................#.......#.##.#.....................#..#..#..#.............#..............#. | ||
...........#...#.........#.#....#................#.....#........#..#...#.#.....#..........#....................#...#..#............ | ||
...........#.#...#....#.....#..#..........#...#.#..#..#.#.........#.#.##........#......#................#...........#.............. | ||
................................................#..#..#....#.........#.............#....###..#...............#...#.......#......... | ||
............................#...#..#......##......#...#........#....#.....#.....#...#..#...#..........................###.#........ | ||
..............#............#...#.#...##.......##.....#.##..........#....#............#.......#......##.#....#.......#.............. | ||
...........#.##.##..#.#.#.........###........#...#...#................#.#...............#..#.#........##........#.##.......#....... | ||
.................................................................S................................................................. | ||
.............#.#......#..##..#.............#...........#..........#..##.#...............#..###..............#...#.##.##....#....... | ||
......#......##..#...............#..#............##.......#.#.......##..#...#.....##........#.................#....#......#........ | ||
..........#..........#.#........#....#......#........#.....#.............#....#..............#.#..#...#.............#.............. | ||
...........#........##.#.......#...#....#........#.#.....#........#..#..........#......##.....#...............#....#....#.......... | ||
...........#............#.....#..#.............#..#....#.#.....##............#.......#.......#............#.#..##.................. | ||
...........##..#...#.....##.........##.......#.......#.............#...#......#...##........#..#....#.......##..........#.......... | ||
.............#.......#......#..#...##..#.................................####...#......#.............#..#..#.......#............... | ||
................#............#........#...##......##...................#..#...#.........#..#.........................#............. | ||
.........................#...##.#......#...........#........#........#...............###....#.#........#............#.............. | ||
...#...........##........#..##.....#.....#.##......#....#..#........#.................#.#.........#........##..#..#................ | ||
..#.............#.##....#......#...#.#.......#.......#.................#.....#.....#.............###......#...#............##...... | ||
........................#.....#..##.........#.....#.....#..#........#...#.....#.....#....#......#...##..#...#.............#..#..... | ||
......#........................#.....#..#...#....#......#....#..#.#.#...#.....#..#......#..#..#...#...#..#..#...............#...... | ||
..........#...........#......##.#.#............#...................##.....#..#...##....#..........#........#.............#......... | ||
.#.......................#.....#..........#..##....#....#...............##......#.#..#.###..............................#..#..#.... | ||
.......................#........##......#.#......#......####..........#..#........#.#...........#..#.#..#.....#.................... | ||
....##.....................#..#.......###.................##.......#...#..#.###..#...#..###........#....#.#.............##....#.... | ||
........#..#............#...#.........#........#....#.##.......#...#.......#..#.#.......................................###........ | ||
.......##.....#..............#..#....#..#...............#......#................#................##......#.............#.#......... | ||
....#......##...........###.....##................#...#....##.#......#....................#...##.#.......#............###...#...... | ||
.#...#.........#..........#..#.##.#.#...........#.#.........##....#.#......#........#.....#..#..........##.............#...#....##. | ||
.........#.#.#....#..........#.....#...#..#.#...............#..........#....................##........................#.##....#..#. | ||
........###..#..................#...#.....#.................#.#....#........#................#...#...........................#...#. | ||
.....#.#..#........#.............#.........#.............#..###......#..#.#....#................##.....................##.......... | ||
.#......###....#....#........#.#..#...#.#..#.#...#........................#.........#...#.........#..........#.......#.........#... | ||
..#.....#...........###........#.............#......#......#..........##.......#.#.#....#...#.................#...........#........ | ||
.....#........#..#..#................#......#....#.........#.##.......#...........#...........#.#............#.#....#......##...... | ||
.....#...##.#.....................#......................................#.#..#.#.....#.......#.#............####.#................ | ||
.......#...#......................####..#..#.............#..#.................#.....#...........#............#..#..........#....... | ||
.##.............#...#.#...........................#..........##............#.......#.#......#............#........#................ | ||
.......#.......#.......#................#......#.#.#...#.............#........#........#.....#.#..............#.................... | ||
.#....#......#.#....#.................#.................##..............................#...............#.....###....#......#....#. | ||
........##...............................#........#.............................#.....#..####.............#...#..#.........##.##... | ||
.#.#............#..#..........#.......#......##.#.#..........#.........##.....#...........#..................#......##.......#..... | ||
.................##.....#.#..............#.#.#.###...#.............#.#....#.....##...#.##..#...........#.#.#....#.................. | ||
..#.#..#..#................................#.#.......##.............#.#..........###................#.....##....#.................. | ||
........#........#.##..#.....##.#........#.#...#..#..#.....#...#....#.#.......#...#.....#..............#.........#.#...#........... | ||
........#.....##..#..#......................#................#...............#...#.#.#..........#.#.##......................#...... | ||
........#......#.....#.#.......#............#...........##...............#............#...........#..........#.#..........#.#...... | ||
......###.....#.................###................#......#........#...#.#.#...#....#.#.........#...#...##........#...#............ | ||
..#.##..#..#....#.....##..........#............#................#.#..#.........................#.##..##..........##....#...#.....#. | ||
....#............#.#..#...#......#..............#..#....##...#..#.....#.........#...#...........#..........#.#......#.#...##.###... | ||
....#................#.........#...............##..#...........#.......#.#..#.......................#....#...##........#........... | ||
....#....................##......#...#...........#.......#.#..#..............##.#....................#....#...................#.... | ||
...#..............##......#..............#...........#.......#......#........##.............#.........#...#.#..##.................. | ||
....#........#.#....#....#.#.....#........#..........................#..#.#.#.#.........#..........#.##...#.##..###.....#.......... | ||
.#.......#.#............#..#....#.......#.#............#..........#.#........##.............#...#..#....#..........#..........#.... | ||
...#...#......#..............#...##.......#..........#.....#......##...#..............#................#..............#...##..##... | ||
....#......#.##..##....#.....................#..........#....##......#................#.......#.#..................#......#.#...... | ||
...#...............#..#..........#..#.....#..##............#.......###...........................##................#...#........... | ||
........#....#........#.......#....#..#.#..................#.............##.............#..#.......##..........................#... | ||
.#....................#......#.#........#..................#.##.#..#.....#.........##................................#............. | ||
.......#....................#..#................#......................................##..........#.....#..##..#..........#.#..#.. | ||
..#.....#..#....#.....#......#..##...#.........#.........................................#...#....#.........#...##.......#.#.....#. | ||
...........#..........#..#.#...#...##..#.........#.#...........#....#..............#.............#.......##..#...#....##.......#... | ||
..#.........#.#..#...#.....#......................#..........#........#.......#....................#...##......#.................#. | ||
.#......#...#.#.#......#.#........#.#......#....#....#............................#......####............#.......#..#..........##.. | ||
.#......##..........#...#...........#....#...#.#.................................#.##...#......................#..#..........#.#... | ||
.....#...#.......#...................##.#....#...##............#..##.........#..................#..#......#..........#....#.....##. | ||
.#..................#..........#.....................#.#.........................#...........#...##...#...#...##.#................. | ||
.#..#..#..#..#......#........#........#.......#.#...#.#..................#.......#.......#..#.#......#...#...............#.....#... | ||
.#........#......................#...##.......#..##......................###......#.....##....#..#................#................ | ||
.#...........#.............#..#.....##.........#.##........#.............#....#.#...........#...............#.........#........#... | ||
......#....#.#...###................#....#......#..#........#.........................##.##...#..#..#.#......#..................... | ||
................................................................................................................................... |
51 changes: 51 additions & 0 deletions
51
src/test/kotlin/biz/koziolek/adventofcode/year2023/day21/Day21Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |