|
| 1 | +package com.codefork.aoc2024.day20; |
| 2 | + |
| 3 | +import com.codefork.aoc2024.util.Assert; |
| 4 | +import com.codefork.aoc2024.util.Grid; |
| 5 | +import com.codefork.aoc2024.util.WithIndex; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import java.util.stream.IntStream; |
| 15 | +import java.util.stream.Stream; |
| 16 | + |
| 17 | +public record Racetrack(int height, int width, |
| 18 | + Position start, Position end, |
| 19 | + Set<Position> track, Set<Position> walls, |
| 20 | + List<Position> course) { |
| 21 | + |
| 22 | + public static Racetrack create(Stream<String> data) { |
| 23 | + var racetrack = Grid.parse( |
| 24 | + data, |
| 25 | + () -> new Racetrack(-1, -1, Position.UNSET, Position.UNSET, new HashSet<>(), new HashSet<>(), new ArrayList<>()), |
| 26 | + (acc, x, y, ch) -> { |
| 27 | + var pos = new Position(x, y); |
| 28 | + var newAcc = acc.withHeight(y + 1).withWidth(x + 1); |
| 29 | + // just mutate track and walls here :( |
| 30 | + if (".".equals(ch)) { |
| 31 | + newAcc.track().add(pos); |
| 32 | + return newAcc; |
| 33 | + } else if ("#".equals(ch)) { |
| 34 | + newAcc.walls().add(pos); |
| 35 | + return newAcc; |
| 36 | + } else if ("S".equals(ch)) { |
| 37 | + newAcc.track().add(pos); |
| 38 | + return newAcc.withStart(pos); |
| 39 | + } else if ("E".equals(ch)) { |
| 40 | + newAcc.track().add(pos); |
| 41 | + return newAcc.withEnd(pos); |
| 42 | + } |
| 43 | + throw new RuntimeException("unhandled character: " + ch); |
| 44 | + }); |
| 45 | + return racetrack.withCourse(racetrack.walk()); |
| 46 | + } |
| 47 | + |
| 48 | + public Racetrack withStart(Position newStart) { |
| 49 | + return new Racetrack(height, width, newStart, end, track, walls, course); |
| 50 | + } |
| 51 | + |
| 52 | + public Racetrack withEnd(Position newEnd) { |
| 53 | + return new Racetrack(height, width, start, newEnd, track, walls, course); |
| 54 | + } |
| 55 | + |
| 56 | + public Racetrack withHeight(int newHeight) { |
| 57 | + return new Racetrack(newHeight, width, start, end, track, walls, course); |
| 58 | + } |
| 59 | + |
| 60 | + public Racetrack withWidth(int newWidth) { |
| 61 | + return new Racetrack(height, newWidth, start, end, track, walls, course); |
| 62 | + } |
| 63 | + |
| 64 | + public Racetrack withCourse(List<Position> newCourse) { |
| 65 | + return new Racetrack(height, width, start, end, track, walls, newCourse); |
| 66 | + } |
| 67 | + |
| 68 | + public Set<Position> getTrackNeighbors(Position pos) { |
| 69 | + return Stream.of(pos.decX(), pos.incX(), pos.decY(), pos.incY()) |
| 70 | + .filter(p -> track().contains(p)) |
| 71 | + .collect(Collectors.toSet()); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * find all the positions at distance d from pos. |
| 76 | + * some of these may be invalid positions (i.e. negative coordinates); |
| 77 | + * we filter those out at a separate step |
| 78 | + **/ |
| 79 | + public Set<Position> getPositionsFromDist(Position pos, int d) { |
| 80 | + // this is a diamond shape |
| 81 | + return IntStream.range(pos.x() - d, pos.x() + d + 1) |
| 82 | + .boxed() |
| 83 | + .flatMap(x -> { |
| 84 | + var xDist = pos.x() > x ? pos.x() - x : x - pos.x(); |
| 85 | + var yDist = d - xDist; |
| 86 | + if(yDist > 0) { |
| 87 | + return Stream.of( |
| 88 | + new Position(x, pos.y() + yDist), |
| 89 | + new Position(x, pos.y() - yDist) |
| 90 | + ); |
| 91 | + } else { |
| 92 | + return Stream.of(new Position(x, pos.y())); |
| 93 | + } |
| 94 | + }) |
| 95 | + .collect(Collectors.toSet()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Walk the racetrack, building a path. includes start and end. |
| 100 | + * This recursive version causes a stack overflow. |
| 101 | + */ |
| 102 | + @SuppressWarnings("unused") |
| 103 | + public List<Position> walkRecursive(Position current, List<Position> acc) { |
| 104 | + var last = !acc.isEmpty() ? acc.getLast() : null; |
| 105 | + acc.add(current); |
| 106 | + if (current.equals(end)) { |
| 107 | + return acc; |
| 108 | + } |
| 109 | + var neighbors = getTrackNeighbors(current).stream() |
| 110 | + .filter(neighbor -> !neighbor.equals(last)) |
| 111 | + .toList(); |
| 112 | + Assert.assertEquals(1, neighbors.size()); |
| 113 | + return walkRecursive(neighbors.getFirst(), acc); |
| 114 | + } |
| 115 | + |
| 116 | + public List<Position> walkIterative(Position current, List<Position> acc) { |
| 117 | + var keepGoing = true; |
| 118 | + while (keepGoing) { |
| 119 | + var last = !acc.isEmpty() ? acc.getLast() : null; |
| 120 | + acc.add(current); |
| 121 | + if (!current.equals(end)) { |
| 122 | + var neighbors = getTrackNeighbors(current).stream() |
| 123 | + .filter(neighbor -> !neighbor.equals(last)) |
| 124 | + .toList(); |
| 125 | + Assert.assertEquals(1, neighbors.size()); |
| 126 | + current = neighbors.getFirst(); |
| 127 | + } else { |
| 128 | + keepGoing = false; |
| 129 | + } |
| 130 | + } |
| 131 | + return acc; |
| 132 | + } |
| 133 | + |
| 134 | + public List<Position> walk() { |
| 135 | + return walkIterative(start, new ArrayList<>()); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * find all the cheats if you're allowed exactly picoseconds to walk through walls |
| 140 | + */ |
| 141 | + public Set<Cheat> findCheats(int picoseconds) { |
| 142 | + var regularCourseTime = course.size() - 1; |
| 143 | + //System.out.println("regularCourseTime=" + regularCourseTime); |
| 144 | + return course.stream() |
| 145 | + .map(WithIndex.indexed()) |
| 146 | + .flatMap(posWithIndex -> { |
| 147 | + var i = posWithIndex.index(); |
| 148 | + var pos = posWithIndex.value(); |
| 149 | + |
| 150 | + // find all the positions that actually intersect with |
| 151 | + // the portion of the course still ahead of us |
| 152 | + var courseAhead = new HashSet<>(course.subList(i + 1, course.size())); |
| 153 | + |
| 154 | + var possibleTrackPositions = getPositionsFromDist(pos, picoseconds); |
| 155 | + return possibleTrackPositions.stream() |
| 156 | + .filter(courseAhead::contains) |
| 157 | + .map(p -> new Cheat(pos, p, -1)); |
| 158 | + }) |
| 159 | + .map(cheat -> { |
| 160 | + // populate "saved" |
| 161 | + var cheatStartIndex = course.indexOf(cheat.start()); |
| 162 | + var cheatEndIndex = course.indexOf(cheat.end()); |
| 163 | + |
| 164 | + // we don't need to generate the actual shortened course, just its length |
| 165 | + var shortenedCourseLength = cheatStartIndex + 1 |
| 166 | + + picoseconds - 1 |
| 167 | + + (course.size() - cheatEndIndex); |
| 168 | + |
| 169 | + var saved = regularCourseTime - (shortenedCourseLength - 1); |
| 170 | + |
| 171 | + return cheat.withSaved(saved); |
| 172 | + }) |
| 173 | + .collect(Collectors.toSet()); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * prints out a summary in the format shown in the puzzle instructions. |
| 178 | + * helpful for verifying/debugging |
| 179 | + */ |
| 180 | + public static void printCheatsSummary(Set<Cheat> cheats) { |
| 181 | + var grouped = cheats.stream().collect(Collectors.groupingBy(Cheat::saved)); |
| 182 | + |
| 183 | + var sortedEntries = grouped.entrySet().stream() |
| 184 | + .sorted(Comparator.comparingInt(Map.Entry::getKey)) |
| 185 | + .toList(); |
| 186 | + |
| 187 | + for (var entry : sortedEntries) { |
| 188 | + var count = entry.getValue().size(); |
| 189 | + var saved = entry.getKey(); |
| 190 | + System.out.println("there are " + count + " cheats that save " + saved + " picoseconds"); |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments