|
| 1 | +package day18 |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +const MAX_THREADS_PER_LOOP = 32 |
| 10 | + |
| 11 | +type point struct { |
| 12 | + x, y int |
| 13 | +} |
| 14 | + |
| 15 | +type maze struct { |
| 16 | + walls map[point]bool |
| 17 | + start point |
| 18 | + end point |
| 19 | + width int |
| 20 | + height int |
| 21 | +} |
| 22 | + |
| 23 | +func (m maze) valid(p point) bool { |
| 24 | + return p.x >= 0 && p.y >= 0 && p.x < m.width && p.y < m.height |
| 25 | +} |
| 26 | + |
| 27 | +type thread struct { |
| 28 | + p point |
| 29 | + moves uint |
| 30 | +} |
| 31 | + |
| 32 | +func Solve(input string, size int, square int) uint { |
| 33 | + m := maze{width: square, height: square, start: point{0, 0}, end: point{x: square - 1, y: square - 1}, walls: make(map[point]bool)} |
| 34 | + blocks := parseInput(input) |
| 35 | + for i := 0; i < size; i++ { |
| 36 | + m.walls[blocks[i]] = true |
| 37 | + } |
| 38 | + return solve(m) |
| 39 | +} |
| 40 | + |
| 41 | +func parseInput(input string) []point { |
| 42 | + result := make([]point, 0) |
| 43 | + for _, line := range strings.Split(input, "\n") { |
| 44 | + d := strings.Split(line, ",") |
| 45 | + if len(d) != 2 { |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + x, _ := strconv.Atoi(d[0]) |
| 50 | + y, _ := strconv.Atoi(d[1]) |
| 51 | + result = append(result, point{x: x, y: y}) |
| 52 | + } |
| 53 | + return result |
| 54 | +} |
| 55 | + |
| 56 | +func solve(m maze) uint { |
| 57 | + seenSpaces := make(map[point]uint) |
| 58 | + seenSpaces[m.start] = 1 |
| 59 | + threads := []thread{{p: m.start}} |
| 60 | + coldStorage := make([]thread, 0) |
| 61 | + var solution uint = 0 |
| 62 | + loops := 0 |
| 63 | + var threadCount uint |
| 64 | + for { |
| 65 | + nextThreads := make([]thread, 0) |
| 66 | + threadCount += uint(len(threads)) |
| 67 | + for i, t := range threads { |
| 68 | + if i >= MAX_THREADS_PER_LOOP { |
| 69 | + coldStorage = append(coldStorage, t) |
| 70 | + continue |
| 71 | + } else if t.p == m.end { |
| 72 | + if solution == 0 || t.moves < solution { |
| 73 | + solution = t.moves |
| 74 | + } |
| 75 | + } else { |
| 76 | + nextThreads = append(calcNextMoves(m, t, seenSpaces), nextThreads...) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if len(nextThreads) == 0 { |
| 81 | + if len(coldStorage) > 0 { |
| 82 | + x := MAX_THREADS_PER_LOOP |
| 83 | + if len(coldStorage) < x { |
| 84 | + x = len(coldStorage) |
| 85 | + } |
| 86 | + nextThreads = coldStorage[:x] |
| 87 | + coldStorage = slices.Delete(coldStorage, 0, x) |
| 88 | + } else { |
| 89 | + if solution == 0 { |
| 90 | + panic("No solution with no more spaces to check") |
| 91 | + } |
| 92 | + return solution |
| 93 | + } |
| 94 | + } |
| 95 | + for i, t := range nextThreads { |
| 96 | + if solution > 0 && t.moves >= solution { |
| 97 | + nextThreads = slices.Delete(nextThreads, i, i) |
| 98 | + } |
| 99 | + if seenSpaces[t.p] == 0 || t.moves < seenSpaces[t.p] { |
| 100 | + seenSpaces[t.p] = t.moves |
| 101 | + } |
| 102 | + } |
| 103 | + threads = nextThreads |
| 104 | + loops++ |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func calcNextMoves(m maze, curr thread, seenSpaces map[point]uint) []thread { |
| 109 | + nextMoves := make([]thread, 0) |
| 110 | + possibilities := []thread{{p: point{x: curr.p.x + 1, y: curr.p.y}, moves: curr.moves}, |
| 111 | + {p: point{x: curr.p.x, y: curr.p.y + 1}, moves: curr.moves}, |
| 112 | + {p: point{x: curr.p.x - 1, y: curr.p.y}, moves: curr.moves}, |
| 113 | + {p: point{x: curr.p.x, y: curr.p.y - 1}, moves: curr.moves}} |
| 114 | + for _, t := range possibilities { |
| 115 | + if m.valid(t.p) && !m.walls[t.p] { |
| 116 | + t.moves++ |
| 117 | + if seenSpaces[t.p] == 0 || t.moves < seenSpaces[t.p] { |
| 118 | + nextMoves = append(nextMoves, t) |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + return nextMoves |
| 123 | +} |
0 commit comments