|
1 | 1 | package de.ronny_h.aoc.year24.day21 |
2 | 2 |
|
3 | 3 | import de.ronny_h.aoc.AdventOfCode |
4 | | -import de.ronny_h.aoc.extensions.Coordinates |
5 | 4 | import de.ronny_h.aoc.extensions.Direction |
6 | | -import de.ronny_h.aoc.extensions.Grid |
7 | | -import de.ronny_h.aoc.extensions.ShortestPath |
8 | | -import de.ronny_h.aoc.extensions.aStar |
| 5 | +import de.ronny_h.aoc.extensions.Direction.* |
| 6 | +import de.ronny_h.aoc.extensions.SimpleCharGrid |
9 | 7 | import de.ronny_h.aoc.extensions.asList |
10 | 8 |
|
11 | | -fun main() = KeypadConundrum().run(0, 0) |
| 9 | +fun main() = KeypadConundrum().run(94426, 0) |
12 | 10 |
|
13 | 11 | class KeypadConundrum : AdventOfCode<Int>(2024, 21) { |
14 | | - override fun part1(input: List<String>): Int { |
15 | | - println("directionalKeypad: $directionalKeypadLayout") |
16 | 12 |
|
17 | | - val numericKeypad = Keypad(numericKeypadLayout) |
18 | | - val sequence = input.map { code -> |
19 | | - val sequence1 = code.map { position -> |
20 | | - numericKeypad.moveTo(position) |
21 | | - }.joinToString("") |
22 | | - println("robot1: $sequence1") |
23 | | - |
24 | | - val directionalKeypad1 = Keypad(directionalKeypadLayout) |
25 | | - val sequence2 = sequence1.map { direction -> |
26 | | - directionalKeypad1.moveTo(direction) |
27 | | - }.joinToString("") |
28 | | - println("robot2: $sequence2") |
| 13 | + data class Node(val code: String, val children: List<Node>) { |
| 14 | + fun collectSequences(level: Int = 0): String { |
| 15 | + if (children.isEmpty()) { |
| 16 | + check(level % 2 == 0) |
| 17 | + return code |
| 18 | + } |
| 19 | + return if (level % 2 == 0) { |
| 20 | + // direct children = sequence |
| 21 | + children.joinToString("") { it.collectSequences(level + 1) } |
| 22 | + } else { |
| 23 | + // direct children = different options (branches in the tree) |
| 24 | + children |
| 25 | + .map { it.collectSequences(level + 1) } |
| 26 | + .minBy { it.length } |
| 27 | + } |
| 28 | + } |
| 29 | + } |
29 | 30 |
|
30 | | - val directionalKeypad2 = Keypad(directionalKeypadLayout) |
31 | | - val sequence3 = sequence2.map { direction -> |
32 | | - directionalKeypad2.moveTo(direction) |
33 | | - }.joinToString("") |
34 | | - println("$code: $sequence3") |
| 31 | + fun input(code: String, keypad: Keypad, depth: Int): Node { |
| 32 | + if (depth == 0) { |
| 33 | + return Node(code, emptyList()) |
35 | 34 | } |
| 35 | + return Node(code, code.map { char -> |
| 36 | + Node("$char", keypad.moveTo(char).map { |
| 37 | + input(it, Keypad(directionalKeypadLayout), depth - 1) |
| 38 | + }) |
| 39 | + }) |
| 40 | + } |
36 | 41 |
|
37 | | - return 0 |
| 42 | + override fun part1(input: List<String>): Int = input.sumOf { |
| 43 | + input(it, Keypad(numericKeypadLayout), 3).collectSequences().length * it.dropLast(1).toInt() |
38 | 44 | } |
39 | 45 |
|
40 | 46 | override fun part2(input: List<String>): Int { |
@@ -72,46 +78,28 @@ val directionalKeypadLayout = """ |
72 | 78 | <v> |
73 | 79 | """.asList() |
74 | 80 |
|
75 | | -class Keypad(layout: List<String>) : Grid<Char>(layout, ' ') { |
76 | | - override fun Char.toElementType(): Char = this |
77 | | - |
78 | | - data class Node(val direction: Direction, val position: Coordinates) { |
79 | | - override fun toString() = "$position$direction" |
80 | | - } |
| 81 | +class Keypad(layout: List<String>) : SimpleCharGrid(layout, ' ') { |
| 82 | + override fun toString(): String = toString(setOf(currentPosition), 'X') |
81 | 83 |
|
82 | 84 | private var currentPosition = find('A') |
83 | 85 |
|
84 | | - fun moveTo(target: Char): String { |
| 86 | + fun moveTo(target: Char): List<String> { |
85 | 87 | val targetPosition = find(target) |
86 | | - val path = shortestPath(currentPosition, targetPosition) |
| 88 | + val paths = shortestPaths(currentPosition, targetPosition) |
87 | 89 | currentPosition = targetPosition |
88 | | - val directions = path.path.drop(1).joinToString("") { |
89 | | - when (it.direction) { |
90 | | - Direction.NORTH -> "^" |
91 | | - Direction.EAST -> ">" |
92 | | - Direction.SOUTH -> "v" |
93 | | - Direction.WEST -> "<" |
94 | | - } |
| 90 | + val directions = paths.map { shortestPath -> |
| 91 | + shortestPath.path |
| 92 | + .windowed(2) |
| 93 | + .map { (from, to) -> Direction.entries.first { from + it == to } } |
| 94 | + .joinToString("") { |
| 95 | + when (it) { |
| 96 | + NORTH -> "^" |
| 97 | + EAST -> ">" |
| 98 | + SOUTH -> "v" |
| 99 | + WEST -> "<" |
| 100 | + } |
| 101 | + } |
95 | 102 | } |
96 | | - return "${directions}A" |
97 | | - } |
98 | | - |
99 | | - fun shortestPath(start: Coordinates, goal: Coordinates): ShortestPath<Node> { |
100 | | - |
101 | | - val neighbours: (Node) -> List<Node> = { node -> |
102 | | - Direction |
103 | | - .entries |
104 | | - .map { Node(it, node.position + it) } |
105 | | - .filter { getAt(it.position) != nullElement } |
106 | | - } |
107 | | - |
108 | | - val d: (Node, Node) -> Int = { a, b -> |
109 | | - // pre-condition: a and b are neighbours |
110 | | - 1 |
111 | | - } |
112 | | - |
113 | | - val h: (Node) -> Int = { it.position taxiDistanceTo goal } |
114 | | - |
115 | | - return aStar(Node(Direction.EAST, start), { this.position == goal }, neighbours, d, h) |
| 103 | + return directions.map { "${it}A" } |
116 | 104 | } |
117 | 105 | } |
0 commit comments