11package de.havox_design.aoc2023.day10
22
33class Day10 (private var filename : String ) {
4+ private val ICON_START = ' S'
5+ private val ICON_VERTICAL_PIPE = ' |'
6+ private val ICON_HORIZONTAL_PIPE = ' -'
7+ private val ICON_NORTH_EAST_BEND = ' L'
8+ private val ICON_NORTH_WEST_BEND = ' J'
9+ private val ICON_SOUTH_WEST_BEND = ' 7'
10+ private val ICON_SOUTH_EAST_BEND = ' F'
11+ private val ICON_FREE_GROUND = ' .'
12+
413 fun solvePart1 (): Long =
5- 0L
14+ solvePart1(
15+ getResourceAsText(filename)
16+ .map { row -> row.toCharArray().toList() }
17+ )
618
719 fun solvePart2 (): Long =
820 0L
921
22+ private fun solvePart1 (maze : List <List <Char >>): Long {
23+ var start: Pair <Int , Int >? = null
24+
25+ out @ for (row in maze.indices) {
26+ for (col in maze[row].indices) {
27+ if (maze[row][col] == ICON_START ) {
28+ start = Pair (row, col)
29+ break @out
30+ }
31+ }
32+ }
33+
34+ val loop = getLoop(start!! , maze)
35+
36+ return (loop.size / 2 ).toLong()
37+ }
38+
39+ private fun getLoop (start : Pair <Int , Int >, maze : List <List <Char >>): List <Pair <Int , Int >> {
40+ val loop = mutableListOf (start)
41+
42+ do {
43+ val adjacentCells = maze
44+ .connectedCells(loop.last())
45+ .filter { ! loop.contains(it) }
46+ .take(1 )
47+
48+ loop.addAll(adjacentCells)
49+ } while (adjacentCells.isNotEmpty())
50+
51+ return loop
52+ }
53+
54+ private fun List<List<Char>>.connectedCells (currentLocation : Pair <Int , Int >): List <Pair <Int , Int >> {
55+ return when (this [currentLocation.first][currentLocation.second]) {
56+ ICON_VERTICAL_PIPE -> listOf (
57+ currentLocation.getAdjacentField(Direction .NORTH ),
58+ currentLocation.getAdjacentField(Direction .SOUTH )
59+ )
60+
61+ ICON_HORIZONTAL_PIPE -> listOf (
62+ currentLocation.getAdjacentField(Direction .WEST ),
63+ currentLocation.getAdjacentField(Direction .EAST )
64+ )
65+
66+ ICON_NORTH_EAST_BEND -> listOf (
67+ currentLocation.getAdjacentField(Direction .NORTH ),
68+ currentLocation.getAdjacentField(Direction .EAST )
69+ )
70+
71+ ICON_NORTH_WEST_BEND -> listOf (
72+ currentLocation.getAdjacentField(Direction .NORTH ),
73+ currentLocation.getAdjacentField(Direction .WEST )
74+ )
75+
76+ ICON_SOUTH_WEST_BEND -> listOf (
77+ currentLocation.getAdjacentField(Direction .WEST ),
78+ currentLocation.getAdjacentField(Direction .SOUTH )
79+ )
80+
81+ ICON_SOUTH_EAST_BEND -> listOf (
82+ currentLocation.getAdjacentField(Direction .EAST ),
83+ currentLocation.getAdjacentField(Direction .SOUTH )
84+ )
85+
86+ ICON_START -> listOf (
87+ currentLocation.getAdjacentField(Direction .NORTH ),
88+ currentLocation.getAdjacentField(Direction .WEST ),
89+ currentLocation.getAdjacentField(Direction .EAST ),
90+ currentLocation.getAdjacentField(Direction .SOUTH )
91+ )
92+ .filter { (it.first in indices) && (it.second in this [it.first].indices) }
93+ .filter { this .connectedCells(it).any { next -> next == currentLocation } }
94+
95+ else -> emptyList()
96+ }.filter { (it.first in indices) && (it.second in this [it.first].indices) }
97+ }
98+
99+ private fun Pair <Int , Int >.getAdjacentField (direction : Direction ): Pair <Int , Int > {
100+ return when (direction) {
101+ Direction .NORTH -> Pair (this .first - 1 , this .second)
102+ Direction .SOUTH -> Pair (this .first + 1 , this .second)
103+ Direction .WEST -> Pair (this .first, this .second - 1 )
104+ Direction .EAST -> Pair (this .first, this .second + 1 )
105+ Direction .NONE -> this
106+ }
107+ }
108+
10109 private fun getResourceAsText (path : String ): List <String > =
11110 this .javaClass.classLoader.getResourceAsStream(path)!! .bufferedReader().readLines()
12111}
0 commit comments