@@ -59,19 +59,7 @@ fun main() {
5959 )
6060
6161 val input = readInput(day)
62- printAndCheck(input, ::part1, 89471 )
63- // too high: 89472
64- // too high: 89471
65- // not right: 88971
66- // not right: 88721
67- // too low: 88472
68-
69-
70- println (" $day part 2" )
71-
72- fun part2 (input : List <String >) = input.size
73-
74- printAndCheck(input, ::part2, 6512 )
62+ printAndCheck(input, ::part1, 89460 )
7563}
7664
7765private class ReindeerMaze (input : List <String >) : Grid<Char>(input) {
@@ -80,15 +68,6 @@ private class ReindeerMaze(input: List<String>) : Grid<Char>(input) {
8068 override fun Char.toElementType () = this
8169
8270 data class Node (val direction : Direction , val position : Coordinates ) {
83- // hashCode and equals are used to determine if a Node was already visited.
84- // -> don't distinguish directions
85- override fun hashCode () = position.hashCode()
86- override fun equals (other : Any? ): Boolean {
87- if (other !is Node ) {
88- return false
89- }
90- return position == other.position
91- }
9271 override fun toString () = " $position$direction "
9372 }
9473
@@ -102,23 +81,22 @@ private class ReindeerMaze(input: List<String>) : Grid<Char>(input) {
10281 val goal = Node (Direction .EAST , Coordinates (1 , width - 2 ))
10382
10483 val neighbours: (Node ) -> List <Node > = { n ->
105- n.position.directedNeighbours()
106- .filter { ! it.first.isOpposite(n.direction) } // don't go back
107- .filter { getAt(it.second) != wall }
108- .map { Node (it.first, it.second) }
84+ listOf (
85+ Node (n.direction, n.position + n.direction),
86+ Node (n.direction.turnLeft(), n.position),
87+ Node (n.direction.turnRight(), n.position)
88+ ).filter { getAt(it.position) != wall }
10989 }
11090
11191 val d: (Node , Node ) -> Int = { a, b ->
112- require(a.position in b.position.neighbours())
11392 if (a.position == goal.position && b.position == goal.position) {
11493 // direction at goal doesn't care
11594 0
116- } else if (a.direction - b.direction == 0 ) {
95+ } else if (a.direction == b.direction) {
11796 // straight ahead
11897 1
11998 } else {
120- // turn left, right or u-turn -> turn cost + move cost
121- (a.direction - b.direction) * 1000 + 1
99+ 1000
122100 }
123101 }
124102
0 commit comments