|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "os" |
| 6 | +) |
| 7 | + |
| 8 | +type Direction uint8 |
| 9 | + |
| 10 | +const ( |
| 11 | + N Direction = iota |
| 12 | + E |
| 13 | + S |
| 14 | + W |
| 15 | +) |
| 16 | + |
| 17 | +type P struct{ x, y int } |
| 18 | + |
| 19 | +var delta = map[Direction]P{ |
| 20 | + N: {-1, 0}, |
| 21 | + E: {0, 1}, |
| 22 | + S: {1, 0}, |
| 23 | + W: {0, -1}, |
| 24 | +} |
| 25 | + |
| 26 | +func (p P) Step(dir Direction) P { |
| 27 | + return P{p.x + delta[dir].x, p.y + delta[dir].y} |
| 28 | +} |
| 29 | + |
| 30 | +type Grid struct { |
| 31 | + Heat [][]int |
| 32 | + Size int |
| 33 | +} |
| 34 | + |
| 35 | +func (g Grid) Contains(p P) bool { |
| 36 | + return p.x >= 0 && p.x < g.Size && p.y >= 0 && p.y < g.Size |
| 37 | +} |
| 38 | + |
| 39 | +func (g Grid) Goal(p P) bool { |
| 40 | + return p.x == g.Size-1 && p.y == g.Size-1 |
| 41 | +} |
| 42 | + |
| 43 | +func (g Grid) HeatLoss(p P) int { |
| 44 | + return g.Heat[p.x][p.y] |
| 45 | +} |
| 46 | + |
| 47 | +func parse() Grid { |
| 48 | + scanner := bufio.NewScanner(os.Stdin) |
| 49 | + |
| 50 | + g := Grid{} |
| 51 | + |
| 52 | + for scanner.Scan() { |
| 53 | + line := scanner.Text() |
| 54 | + var row []int |
| 55 | + for c := 0; c < len(line); c++ { |
| 56 | + row = append(row, int(line[c]-'0')) |
| 57 | + } |
| 58 | + g.Heat = append(g.Heat, row) |
| 59 | + } |
| 60 | + g.Size = len(g.Heat) |
| 61 | + return g |
| 62 | +} |
| 63 | + |
| 64 | +func PossibleDirections(curr Direction, streak, minSteps, maxSteps int) []Direction { |
| 65 | + if streak < minSteps { |
| 66 | + return []Direction{ |
| 67 | + curr, // straight |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if streak >= maxSteps { |
| 72 | + return []Direction{ |
| 73 | + (curr + 1) % 4, // turn right |
| 74 | + (curr + 3) % 4, // turn left |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return []Direction{ |
| 79 | + (curr + 1) % 4, // turn right |
| 80 | + curr, // straight |
| 81 | + (curr + 3) % 4, // turn left |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +type Node struct { |
| 86 | + Pos P |
| 87 | + Dir Direction |
| 88 | + Streak int |
| 89 | +} |
| 90 | + |
| 91 | +func Dijkstra(g Grid, minSteps, maxSteps int) int { |
| 92 | + |
| 93 | + // starting nodes |
| 94 | + startNode := Node{P{0, 0}, E, 0} |
| 95 | + startNode2 := Node{P{0, 0}, S, 0} |
| 96 | + visited := map[Node]int{startNode: 0, startNode2: 0} |
| 97 | + |
| 98 | + pq := NewPriorityQueue() |
| 99 | + pq.Insert(startNode, 0) |
| 100 | + pq.Insert(startNode2, 0) |
| 101 | + |
| 102 | + for pq.Len() > 0 { |
| 103 | + currentNode, currentHeatLoss := pq.PopMin() |
| 104 | + |
| 105 | + if g.Goal(currentNode.Pos) { |
| 106 | + return currentHeatLoss |
| 107 | + } |
| 108 | + |
| 109 | + for _, nextDir := range PossibleDirections(currentNode.Dir, currentNode.Streak, minSteps, maxSteps) { |
| 110 | + |
| 111 | + nextStreak := 1 |
| 112 | + if nextDir == currentNode.Dir { |
| 113 | + nextStreak = currentNode.Streak + 1 |
| 114 | + } |
| 115 | + |
| 116 | + nextPos := currentNode.Pos.Step(nextDir) |
| 117 | + |
| 118 | + // ignore points out of bound |
| 119 | + if !g.Contains(nextPos) { |
| 120 | + continue |
| 121 | + } |
| 122 | + |
| 123 | + nextNode := Node{ |
| 124 | + Pos: nextPos, |
| 125 | + Dir: nextDir, |
| 126 | + Streak: nextStreak, |
| 127 | + } |
| 128 | + nextHeatLoss := currentHeatLoss + g.HeatLoss(nextPos) |
| 129 | + |
| 130 | + if oldHeatLoss, ok := visited[nextNode]; ok && oldHeatLoss <= nextHeatLoss { |
| 131 | + continue |
| 132 | + } |
| 133 | + |
| 134 | + visited[nextNode] = nextHeatLoss |
| 135 | + pq.Insert(nextNode, nextHeatLoss) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return 0 |
| 140 | +} |
0 commit comments