|
| 1 | +import de.ronny_h.extensions.Coordinates |
| 2 | +import de.ronny_h.extensions.Direction |
| 3 | +import de.ronny_h.extensions.Grid |
| 4 | +import de.ronny_h.extensions.aStar |
| 5 | + |
| 6 | +fun main() { |
| 7 | + val day = "Day16" |
| 8 | + |
| 9 | + println("$day part 1") |
| 10 | + |
| 11 | + fun part1(input: List<String>): Int { |
| 12 | + val maze = ReindeerMaze(input) |
| 13 | + maze.printGrid() |
| 14 | + return maze.calculateLowestScore() |
| 15 | + } |
| 16 | + |
| 17 | + printAndCheck( |
| 18 | + """ |
| 19 | + ############### |
| 20 | + #.......#....E# |
| 21 | + #.#.###.#.###.# |
| 22 | + #.....#.#...#.# |
| 23 | + #.###.#####.#.# |
| 24 | + #.#.#.......#.# |
| 25 | + #.#.#####.###.# |
| 26 | + #...........#.# |
| 27 | + ###.#.#####.#.# |
| 28 | + #...#.....#.#.# |
| 29 | + #.#.#.###.#.#.# |
| 30 | + #.....#...#.#.# |
| 31 | + #.###.#.#.#.#.# |
| 32 | + #S..#.....#...# |
| 33 | + ############### |
| 34 | + """.trimIndent().lines(), |
| 35 | + ::part1, 7036 |
| 36 | + ) |
| 37 | + |
| 38 | + printAndCheck( |
| 39 | + """ |
| 40 | + ################# |
| 41 | + #...#...#...#..E# |
| 42 | + #.#.#.#.#.#.#.#.# |
| 43 | + #.#.#.#...#...#.# |
| 44 | + #.#.#.#.###.#.#.# |
| 45 | + #...#.#.#.....#.# |
| 46 | + #.#.#.#.#.#####.# |
| 47 | + #.#...#.#.#.....# |
| 48 | + #.#.#####.#.###.# |
| 49 | + #.#.#.......#...# |
| 50 | + #.#.###.#####.### |
| 51 | + #.#.#...#.....#.# |
| 52 | + #.#.#.#####.###.# |
| 53 | + #.#.#.........#.# |
| 54 | + #.#.#.#########.# |
| 55 | + #S#.............# |
| 56 | + ################# |
| 57 | + """.trimIndent().lines(), |
| 58 | + ::part1, 11048 |
| 59 | + ) |
| 60 | + |
| 61 | + val input = readInput(day) |
| 62 | + printAndCheck(input, ::part1, 89471) |
| 63 | + // too high: 89472 |
| 64 | + // too high: 89471 |
| 65 | + // not right: 88971 |
| 66 | + // too low: 88472 |
| 67 | + |
| 68 | + |
| 69 | + println("$day part 2") |
| 70 | + |
| 71 | + fun part2(input: List<String>) = input.size |
| 72 | + |
| 73 | + printAndCheck(input, ::part2, 6512) |
| 74 | +} |
| 75 | + |
| 76 | +private class ReindeerMaze(input: List<String>) : Grid<Char>(input) { |
| 77 | + private val wall = '#' |
| 78 | + override val nullElement = wall |
| 79 | + override fun Char.toElementType() = this |
| 80 | + |
| 81 | + data class Node(val direction: Direction, val position: Coordinates) { |
| 82 | + // hashCode and equals are used to determine if a Node was already visited. |
| 83 | + // -> don't distinguish directions |
| 84 | + override fun hashCode() = position.hashCode() |
| 85 | + override fun equals(other: Any?): Boolean { |
| 86 | + if (other !is Node) { |
| 87 | + return false |
| 88 | + } |
| 89 | + return position == other.position |
| 90 | + } |
| 91 | + override fun toString() = "$position$direction" |
| 92 | + } |
| 93 | + |
| 94 | + fun calculateLowestScore(): Int { |
| 95 | + // A* algorithm |
| 96 | + // - heuristic function: manhattan distance |
| 97 | + // - weight function: |
| 98 | + // * 1 for going forward |
| 99 | + // * 1000 for each 90° turn |
| 100 | + val start = Node(Direction.EAST, Coordinates(height - 2, 1)) |
| 101 | + val goal = Node(Direction.EAST, Coordinates(1, width - 2)) |
| 102 | + |
| 103 | + val neighbours: (Node) -> List<Node> = { n -> |
| 104 | + n.position.directedNeighbours() |
| 105 | + .filter { !it.first.isOpposite(n.direction) } // don't go back |
| 106 | + .filter { getAt(it.second) != wall } |
| 107 | + .map { Node(it.first, it.second) } |
| 108 | + } |
| 109 | + |
| 110 | + val d: (Node, Node) -> Int = { a, b -> |
| 111 | + require(a.position in b.position.neighbours()) |
| 112 | + if (a.position == goal.position && b.position == goal.position) { |
| 113 | + // direction at goal doesn't care |
| 114 | + 0 |
| 115 | + } else if (a.direction - b.direction == 0) { |
| 116 | + // straight ahead |
| 117 | + 1 |
| 118 | + } else { |
| 119 | + // turn left, right or u-turn -> turn cost + move cost |
| 120 | + (a.direction - b.direction) * 1000 + 1 |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + val h: (Node) -> Int = { n -> n.position taxiDistanceTo goal.position } |
| 125 | + |
| 126 | + val printIt: (Set<Node>, Node, () -> String) -> Unit = { visited, current, info -> |
| 127 | + printGrid(visited.map { it.position }.toSet(), 'o', current.position, current.direction) |
| 128 | + println(info.invoke()) |
| 129 | + } |
| 130 | + |
| 131 | + val shortestPath = aStar(start, goal, neighbours, d, h) //, printIt) |
| 132 | + printGrid(path = shortestPath.path.associate { it.position to it.direction.asChar() }) |
| 133 | + return shortestPath.distance |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments