|
1 | 1 | package org.ck.adventofcode.year2024;
|
2 | 2 |
|
3 |
| -import java.util.Scanner; |
| 3 | +import java.util.*; |
| 4 | +import java.util.stream.Collectors; |
| 5 | +import java.util.stream.Stream; |
4 | 6 | import org.ck.adventofcode.util.AOCSolution;
|
5 | 7 | import org.ck.codechallengelib.annotation.Solution;
|
6 | 8 |
|
7 | 9 | @Solution(
|
8 | 10 | id = 20242101,
|
9 |
| - name = "Day 21: ?", |
| 11 | + name = "Day 21: Keypad Conundrum", |
10 | 12 | url = "https://adventofcode.com/2024/day/21",
|
11 |
| - category = "2024", |
12 |
| - solved = false) |
| 13 | + category = "2024") |
13 | 14 | @Solution(
|
14 | 15 | id = 20242102,
|
15 |
| - name = "Day 21: ? - Part 2", |
| 16 | + name = "Day 21: Keypad Conundrum - Part 2", |
16 | 17 | url = "https://adventofcode.com/2024/day/21#part2",
|
17 |
| - category = "2024", |
18 |
| - solved = false) |
| 18 | + category = "2024") |
19 | 19 | public class Day21 extends AOCSolution {
|
| 20 | + private static final Map<Character, Position> NUM_PAD = new HashMap<>(); |
| 21 | + private static final Map<Character, Position> DIR_PAD = new HashMap<>(); |
| 22 | + |
| 23 | + static { |
| 24 | + initNumPadMovements(); |
| 25 | + initDirPadMovements(); |
| 26 | + } |
| 27 | + |
| 28 | + private static void initNumPadMovements() { |
| 29 | + NUM_PAD.put('0', new Position(1, 0)); |
| 30 | + NUM_PAD.put('1', new Position(0, 1)); |
| 31 | + NUM_PAD.put('2', new Position(1, 1)); |
| 32 | + NUM_PAD.put('3', new Position(2, 1)); |
| 33 | + NUM_PAD.put('4', new Position(0, 2)); |
| 34 | + NUM_PAD.put('5', new Position(1, 2)); |
| 35 | + NUM_PAD.put('6', new Position(2, 2)); |
| 36 | + NUM_PAD.put('7', new Position(0, 3)); |
| 37 | + NUM_PAD.put('8', new Position(1, 3)); |
| 38 | + NUM_PAD.put('9', new Position(2, 3)); |
| 39 | + NUM_PAD.put('A', new Position(2, 0)); |
| 40 | + } |
| 41 | + |
| 42 | + private static void initDirPadMovements() { |
| 43 | + DIR_PAD.put('^', new Position(1, 1)); |
| 44 | + DIR_PAD.put('<', new Position(0, 0)); |
| 45 | + DIR_PAD.put('v', new Position(1, 0)); |
| 46 | + DIR_PAD.put('>', new Position(2, 0)); |
| 47 | + DIR_PAD.put('A', new Position(2, 1)); |
| 48 | + } |
20 | 49 |
|
21 | 50 | @Override
|
22 | 51 | protected void runPartOne(final Scanner in) {
|
23 |
| - run(in); |
| 52 | + run(in, 2); |
24 | 53 | }
|
25 | 54 |
|
26 | 55 | @Override
|
27 | 56 | protected void runPartTwo(final Scanner in) {
|
28 |
| - run(in); |
| 57 | + run(in, 25); |
29 | 58 | }
|
30 | 59 |
|
31 |
| - private void run(final Scanner in) { |
32 |
| - print("Whoopsie"); |
| 60 | + private void run(final Scanner in, final int intermediateRobots) { |
| 61 | + final List<String> codes = new ArrayList<>(); |
| 62 | + while (in.hasNextLine()) { |
| 63 | + codes.add(in.nextLine()); |
| 64 | + } |
| 65 | + |
| 66 | + long result = 0; |
| 67 | + for (String code : codes) { |
| 68 | + Position currentPosition = NUM_PAD.get('A'); |
| 69 | + final StringBuilder keyPresses = new StringBuilder(); |
| 70 | + |
| 71 | + for (char c : code.toCharArray()) { |
| 72 | + final Position nextPosition = NUM_PAD.get(c); |
| 73 | + keyPresses.append(getKeyPresses(currentPosition, nextPosition)); |
| 74 | + keyPresses.append('A'); |
| 75 | + |
| 76 | + currentPosition = nextPosition; |
| 77 | + } |
| 78 | + |
| 79 | + final Map<CacheKey, Long> cache = new HashMap<>(); |
| 80 | + |
| 81 | + char current = 'A'; |
| 82 | + long length = 0; |
| 83 | + for (final char next : "%sA".formatted(keyPresses).toCharArray()) { |
| 84 | + length += getKeyPresses(current, next, cache, intermediateRobots - 1); |
| 85 | + current = next; |
| 86 | + } |
| 87 | + |
| 88 | + result += (length - 1) * Long.parseLong(code.replaceAll("\\D", "")); |
| 89 | + } |
| 90 | + |
| 91 | + print(result); |
33 | 92 | }
|
| 93 | + |
| 94 | + private long getKeyPresses( |
| 95 | + final char one, final char two, final Map<CacheKey, Long> cache, int depth) { |
| 96 | + final CacheKey cacheKey = new CacheKey(one, two, depth); |
| 97 | + |
| 98 | + if (cache.containsKey(cacheKey)) { |
| 99 | + return cache.get(cacheKey); |
| 100 | + } |
| 101 | + |
| 102 | + final Set<String> presses = getCombos(DIR_PAD.get(one), DIR_PAD.get(two), 1); |
| 103 | + |
| 104 | + if (depth == 0) { |
| 105 | + return presses.stream().findFirst().orElseThrow().length(); |
| 106 | + } |
| 107 | + |
| 108 | + long min = Long.MAX_VALUE; |
| 109 | + |
| 110 | + for (final String press : presses) { |
| 111 | + char current = 'A'; |
| 112 | + long result = 0; |
| 113 | + for (final char next : "%sA".formatted(press).toCharArray()) { |
| 114 | + result += getKeyPresses(current, next, cache, depth - 1); |
| 115 | + current = next; |
| 116 | + } |
| 117 | + |
| 118 | + min = Math.min(min, result); |
| 119 | + } |
| 120 | + |
| 121 | + cache.put(cacheKey, min - 1); |
| 122 | + return min - 1; |
| 123 | + } |
| 124 | + |
| 125 | + private Set<String> getCombos( |
| 126 | + final Position currentPosition, final Position nextPosition, final int invalidKeyY) { |
| 127 | + final List<Character> keyPresses = new ArrayList<>(); |
| 128 | + |
| 129 | + final int xDiff = currentPosition.x() - nextPosition.x(); |
| 130 | + final int yDiff = currentPosition.y() - nextPosition.y(); |
| 131 | + |
| 132 | + for (int i = yDiff; i < 0; ++i) { |
| 133 | + keyPresses.add('^'); |
| 134 | + } |
| 135 | + |
| 136 | + for (int i = 0; i < yDiff; ++i) { |
| 137 | + keyPresses.add('v'); |
| 138 | + } |
| 139 | + |
| 140 | + for (int i = xDiff; i < 0; ++i) { |
| 141 | + keyPresses.add('>'); |
| 142 | + } |
| 143 | + |
| 144 | + for (int i = 0; i < xDiff; ++i) { |
| 145 | + keyPresses.add('<'); |
| 146 | + } |
| 147 | + |
| 148 | + return shuffle(keyPresses).stream() |
| 149 | + .filter( |
| 150 | + keys -> { |
| 151 | + if (currentPosition.y() == invalidKeyY |
| 152 | + && xDiff != 0 |
| 153 | + && nextPosition.x() == 0 |
| 154 | + && keys.startsWith("<".repeat(xDiff))) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + return !(currentPosition.x() == 0 |
| 159 | + && nextPosition.y() == invalidKeyY |
| 160 | + && (keys.startsWith("^") || keys.startsWith("v"))); |
| 161 | + }) |
| 162 | + .collect(Collectors.toSet()); |
| 163 | + } |
| 164 | + |
| 165 | + private Set<String> shuffle(final List<Character> chars) { |
| 166 | + if (chars.isEmpty()) { |
| 167 | + return Set.of("A"); |
| 168 | + } |
| 169 | + |
| 170 | + if (chars.size() == 1) { |
| 171 | + return Set.of(chars.getFirst() + "A"); |
| 172 | + } |
| 173 | + |
| 174 | + Set<String> result = new HashSet<>(); |
| 175 | + |
| 176 | + for (int i = 0; i < chars.size(); ++i) { |
| 177 | + final List<Character> remaining = |
| 178 | + Stream.concat(chars.stream().limit(i), chars.stream().skip(i + 1L)).toList(); |
| 179 | + |
| 180 | + Set<String> others = shuffle(remaining); |
| 181 | + for (String other : others) { |
| 182 | + result.add(chars.get(i) + other); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return result; |
| 187 | + } |
| 188 | + |
| 189 | + private String getKeyPresses(final Position currentPosition, final Position nextPosition) { |
| 190 | + final StringBuilder keyPresses = new StringBuilder(); |
| 191 | + |
| 192 | + final int xDiff = currentPosition.x() - nextPosition.x(); |
| 193 | + final int yDiff = currentPosition.y() - nextPosition.y(); |
| 194 | + |
| 195 | + if (currentPosition.y() == 0 && nextPosition.x() == 0) { |
| 196 | + if (yDiff < 0) { |
| 197 | + keyPresses.append("^".repeat(-1 * yDiff)); |
| 198 | + } |
| 199 | + |
| 200 | + if (yDiff > 0) { |
| 201 | + keyPresses.append("v".repeat(yDiff)); |
| 202 | + } |
| 203 | + |
| 204 | + if (xDiff > 0) { |
| 205 | + keyPresses.append("<".repeat(xDiff)); |
| 206 | + } |
| 207 | + |
| 208 | + if (xDiff < 0) { |
| 209 | + keyPresses.append(">".repeat(-1 * xDiff)); |
| 210 | + } |
| 211 | + |
| 212 | + return keyPresses.toString(); |
| 213 | + } |
| 214 | + |
| 215 | + if (xDiff > 0) { |
| 216 | + keyPresses.append("<".repeat(xDiff)); |
| 217 | + } |
| 218 | + |
| 219 | + if (yDiff < 0) { |
| 220 | + keyPresses.append("^".repeat(-1 * yDiff)); |
| 221 | + } |
| 222 | + |
| 223 | + if (yDiff > 0) { |
| 224 | + keyPresses.append("v".repeat(yDiff)); |
| 225 | + } |
| 226 | + |
| 227 | + if (xDiff < 0) { |
| 228 | + keyPresses.append(">".repeat(-1 * xDiff)); |
| 229 | + } |
| 230 | + |
| 231 | + return keyPresses.toString(); |
| 232 | + } |
| 233 | + |
| 234 | + private record CacheKey(char start, char end, int depth) {} |
| 235 | + |
| 236 | + private record Position(int x, int y) {} |
34 | 237 | }
|
0 commit comments