|
| 1 | +package day20 |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +const CHEAT_LENGTH_OF_INTEREST = 100 |
| 9 | +const EMPTY_SPACE = '.' |
| 10 | +const END = 'E' |
| 11 | +const START = 'S' |
| 12 | +const MOVES_TO_USE_CHEAT = 2 |
| 13 | +const THREAD_POOL = 64 |
| 14 | + |
| 15 | +type point struct { |
| 16 | + x, y int |
| 17 | +} |
| 18 | + |
| 19 | +type maze struct { |
| 20 | + start, end point |
| 21 | + width, height uint |
| 22 | + emptySpaces map[point]bool |
| 23 | +} |
| 24 | + |
| 25 | +func (m maze) valid(p point) bool { |
| 26 | + return p.x >= 0 && p.y >= 0 && p.x < int(m.width) && p.y < int(m.height) |
| 27 | +} |
| 28 | + |
| 29 | +type thread struct { |
| 30 | + p point |
| 31 | + moves []point |
| 32 | +} |
| 33 | + |
| 34 | +func Solve(input string) uint { |
| 35 | + m, sol := SolvePuzzle(input) |
| 36 | + cheats := Cheats(m, sol) |
| 37 | + var cnt uint = 0 |
| 38 | + for k, v := range cheats { |
| 39 | + if k >= CHEAT_LENGTH_OF_INTEREST { |
| 40 | + cnt += v |
| 41 | + } |
| 42 | + } |
| 43 | + return cnt |
| 44 | +} |
| 45 | + |
| 46 | +func SolvePuzzle(input string) (maze, []point) { |
| 47 | + m := parseInput(input) |
| 48 | + sol := solve(m) |
| 49 | + return m, sol |
| 50 | +} |
| 51 | + |
| 52 | +func Cheats(m maze, sol []point) map[uint]uint { |
| 53 | + cheats := make(map[uint]uint) |
| 54 | + solution := make(map[point]uint) |
| 55 | + for i, p := range sol { |
| 56 | + solution[p] = uint(i) |
| 57 | + } |
| 58 | + |
| 59 | + for i := 1; i < int(m.width-1); i++ { |
| 60 | + for j := 1; j < int(m.height-1); j++ { |
| 61 | + p := point{x: i, y: j} |
| 62 | + if m.emptySpaces[p] || solution[p] > 0 { |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + east, south, west, north := point{x: p.x + 1, y: p.y}, point{x: p.x, y: p.y + 1}, point{x: p.x - 1, y: p.y}, point{x: p.x, y: p.y - 1} |
| 67 | + if (solution[east] == 0 || solution[west] == 0) && (solution[south] == 0 || solution[north] == 0) { |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + cheats[max(cheatLength(east, west, solution), cheatLength(south, north, solution))-MOVES_TO_USE_CHEAT] += 1 |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return cheats |
| 76 | +} |
| 77 | + |
| 78 | +func parseInput(input string) maze { |
| 79 | + var m maze |
| 80 | + m.emptySpaces = make(map[point]bool) |
| 81 | + for j, line := range strings.Split(input, "\n") { |
| 82 | + m.width = uint(len(line)) |
| 83 | + for i, r := range line { |
| 84 | + p := point{x: i, y: j} |
| 85 | + switch r { |
| 86 | + case EMPTY_SPACE: |
| 87 | + m.emptySpaces[p] = true |
| 88 | + case START: |
| 89 | + m.start = p |
| 90 | + case END: |
| 91 | + m.emptySpaces[p] = true |
| 92 | + m.end = p |
| 93 | + } |
| 94 | + } |
| 95 | + m.height = uint(j) + 1 |
| 96 | + } |
| 97 | + return m |
| 98 | +} |
| 99 | + |
| 100 | +func solve(m maze) []point { |
| 101 | + seenSpaces := make(map[point]uint) |
| 102 | + seenSpaces[m.start] = 1 |
| 103 | + threads := []thread{{p: m.start, moves: []point{m.start}}} |
| 104 | + coldStorage := make([]thread, 0) |
| 105 | + solution := make([]point, 0) |
| 106 | + for { |
| 107 | + nextThreads := make([]thread, 0) |
| 108 | + for i, t := range threads { |
| 109 | + if i >= THREAD_POOL { |
| 110 | + coldStorage = append(coldStorage, t) |
| 111 | + continue |
| 112 | + } else if t.p == m.end { |
| 113 | + if len(solution) == 0 || len(t.moves) < len(solution) { |
| 114 | + solution = t.moves |
| 115 | + } |
| 116 | + } else { |
| 117 | + nextThreads = append(calcNextMoves(m, t, seenSpaces), nextThreads...) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if len(nextThreads) == 0 { |
| 122 | + if len(coldStorage) > 0 { |
| 123 | + x := THREAD_POOL |
| 124 | + if len(coldStorage) < x { |
| 125 | + x = len(coldStorage) |
| 126 | + } |
| 127 | + nextThreads = coldStorage[:x] |
| 128 | + coldStorage = slices.Delete(coldStorage, 0, x) |
| 129 | + } else { |
| 130 | + if len(solution) == 0 { |
| 131 | + panic("No solution with no more spaces to check") |
| 132 | + } |
| 133 | + return solution |
| 134 | + } |
| 135 | + } |
| 136 | + for i, t := range nextThreads { |
| 137 | + if len(solution) > 0 && len(t.moves) >= len(solution) { |
| 138 | + nextThreads = slices.Delete(nextThreads, i, i) |
| 139 | + } |
| 140 | + if seenSpaces[t.p] == 0 || uint(len(t.moves)) < seenSpaces[t.p] { |
| 141 | + seenSpaces[t.p] = uint(len(t.moves)) |
| 142 | + } |
| 143 | + } |
| 144 | + threads = nextThreads |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func calcNextMoves(m maze, curr thread, seenSpaces map[point]uint) []thread { |
| 149 | + nextMoves := make([]thread, 0) |
| 150 | + possibilities := []thread{{p: point{x: curr.p.x + 1, y: curr.p.y}, moves: curr.moves}, |
| 151 | + {p: point{x: curr.p.x, y: curr.p.y + 1}, moves: curr.moves}, |
| 152 | + {p: point{x: curr.p.x - 1, y: curr.p.y}, moves: curr.moves}, |
| 153 | + {p: point{x: curr.p.x, y: curr.p.y - 1}, moves: curr.moves}} |
| 154 | + for _, t := range possibilities { |
| 155 | + if m.valid(t.p) && m.emptySpaces[t.p] { |
| 156 | + if seenSpaces[t.p] == 0 || uint(len(t.moves))+1 < seenSpaces[t.p] { |
| 157 | + t.moves = append(t.moves, t.p) |
| 158 | + nextMoves = append(nextMoves, t) |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + return nextMoves |
| 163 | +} |
| 164 | + |
| 165 | +func cheatLength(a, b point, solution map[point]uint) uint { |
| 166 | + if solution[a] == 0 || solution[b] == 0 { |
| 167 | + return 0 |
| 168 | + } |
| 169 | + |
| 170 | + if solution[a] > solution[b] { |
| 171 | + return solution[a] - solution[b] |
| 172 | + } else { |
| 173 | + return solution[b] - solution[a] |
| 174 | + } |
| 175 | +} |
0 commit comments