Skip to content

Commit e74ec2d

Browse files
committed
Solved day10 part 1
1 parent 1e9cbbc commit e74ec2d

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2023'
7-
version '0.9.1'
7+
version '0.9.2'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {
Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,111 @@
11
package de.havox_design.aoc2023.day10
22

33
class 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
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package de.havox_design.aoc2023.day10
2+
3+
enum class Direction {
4+
NORTH,
5+
SOUTH,
6+
WEST,
7+
EAST,
8+
NONE
9+
}

day10/src/test/kotlin/de/havox_design/aoc2023/day10/Day10Test.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class Day10Test {
2727
@JvmStatic
2828
private fun getDataForTestSolvePart1(): Stream<Arguments> =
2929
Stream.of(
30-
Arguments.of("part1sample1.txt", 0L),
31-
Arguments.of("part1sample2.txt", 0L)
30+
Arguments.of("part1sample1.txt", 4L),
31+
Arguments.of("part1sample2.txt", 8L)
3232
)
3333

3434
@JvmStatic

0 commit comments

Comments
 (0)