@@ -7,11 +7,13 @@ import de.ronny_h.extensions.aStar
77fun main () {
88 val day = " Day18"
99
10+ fun List<String>.toCoordinates (): List <Coordinates > = map {
11+ val (x, y) = it.split(' ,' )
12+ Coordinates (x.toInt(), y.toInt())
13+ }
14+
1015 fun part1 (input : List <String >, width : Int ): Int {
11- val memorySpace = MemorySpace (width, input.map {
12- val (x, y) = it.split(' ,' )
13- Coordinates (x.toInt(), y.toInt())
14- })
16+ val memorySpace = MemorySpace (width, input.toCoordinates())
1517 memorySpace.printGrid()
1618 println (" -----------------" )
1719 val shortestPath = memorySpace.shortestPath(Coordinates (0 ,0 ), Coordinates (width- 1 , width- 1 ))
@@ -22,10 +24,21 @@ fun main() {
2224 fun part1Small (input : List <String >) = part1(input, 7 ) // 0..6
2325 fun part1Big (input : List <String >) = part1(input, 71 ) // 0..70
2426
25- fun part2 (input : List <String >): Int {
26- return input.size
27+ fun part2 (input : List <String >, width : Int , startIndex : Int ): String {
28+ for (n in startIndex.. input.lastIndex) {
29+ val memorySpace = MemorySpace (width, input.subList(0 , n).toCoordinates())
30+ try {
31+ memorySpace.shortestPath(Coordinates (0 , 0 ), Coordinates (width - 1 , width - 1 ))
32+ } catch (e: IllegalStateException ) {
33+ return input[n- 1 ]
34+ }
35+ }
36+ return " "
2737 }
2838
39+ fun part2Small (input : List <String >) = part2(input, 7 , 12 ) // 0..6
40+ fun part2Big (input : List <String >) = part2(input, 71 , 1024 ) // 0..70
41+
2942 println (" $day part 1" )
3043
3144 val testInput = readInput(" ${day} _test" )
@@ -37,13 +50,8 @@ fun main() {
3750
3851 println (" $day part 2" )
3952
40- printAndCheck(
41- listOf (
42- " 1 6"
43- ), ::part2, 1
44- )
45- printAndCheck(testInput, ::part2, 31 )
46- printAndCheck(input, ::part2, 18805872 )
53+ printAndCheck(testInput, ::part2Small, " 6,1" )
54+ printAndCheck(input, ::part2Big, " 50,23" )
4755}
4856
4957private class MemorySpace (width : Int , corrupted : List <Coordinates >) : Grid<Char>(width, width, ' .' , ' #' , corrupted) {
0 commit comments