|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "container/heap" |
| 6 | + "math" |
| 7 | + "os" |
| 8 | +) |
| 9 | + |
| 10 | +type P struct{ x, y int } |
| 11 | + |
| 12 | +type Direction uint8 |
| 13 | + |
| 14 | +func (d Direction) Clockwise() Direction { return (d + 4 + 1) % 4 } |
| 15 | +func (d Direction) Counterclockwise() Direction { return (d + 4 - 1) % 4 } |
| 16 | + |
| 17 | +const ( |
| 18 | + E Direction = iota |
| 19 | + S |
| 20 | + W |
| 21 | + N |
| 22 | +) |
| 23 | + |
| 24 | +var Delta = map[Direction]P{N: {-1, 0}, E: {0, 1}, S: {1, 0}, W: {0, -1}} |
| 25 | + |
| 26 | +func parseInput() (map[P]struct{}, P, P) { |
| 27 | + var ( |
| 28 | + m = map[P]struct{}{} |
| 29 | + start P |
| 30 | + end P |
| 31 | + ) |
| 32 | + |
| 33 | + scanner := bufio.NewScanner(os.Stdin) |
| 34 | + for i := 0; scanner.Scan(); i++ { |
| 35 | + for j, ch := range scanner.Text() { |
| 36 | + switch ch { |
| 37 | + case 'S': |
| 38 | + m[P{i, j}] = struct{}{} |
| 39 | + start = P{i, j} |
| 40 | + case 'E': |
| 41 | + m[P{i, j}] = struct{}{} |
| 42 | + end = P{i, j} |
| 43 | + case '.': |
| 44 | + m[P{i, j}] = struct{}{} |
| 45 | + case '#': |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + return m, start, end |
| 50 | +} |
| 51 | + |
| 52 | +type Node struct { |
| 53 | + Pos P |
| 54 | + Direction Direction |
| 55 | +} |
| 56 | + |
| 57 | +func Dijkstra(m map[P]struct{}, start, end P) (int, map[Node][]Node) { |
| 58 | + var ( |
| 59 | + minScore = math.MaxInt64 |
| 60 | + parent = map[Node][]Node{} |
| 61 | + ) |
| 62 | + |
| 63 | + startNode := Node{start, E} |
| 64 | + |
| 65 | + pq := &priorityQueue{} |
| 66 | + heap.Init(pq) |
| 67 | + heap.Push(pq, pqNode{startNode, 0}) |
| 68 | + |
| 69 | + scores := map[Node]int{startNode: 0} |
| 70 | + |
| 71 | + for pq.Len() > 0 { |
| 72 | + curr := heap.Pop(pq).(pqNode) |
| 73 | + |
| 74 | + // we reached the end |
| 75 | + if curr.Pos == end { |
| 76 | + |
| 77 | + // stop only when we got over the minScore, as |
| 78 | + // we are interested into all the minScore paths |
| 79 | + if curr.Score > minScore { |
| 80 | + return minScore, parent |
| 81 | + } |
| 82 | + minScore = curr.Score |
| 83 | + } |
| 84 | + |
| 85 | + for _, n := range neighbours(m, curr) { |
| 86 | + if _, ok := scores[n.Node]; !ok || n.Score <= scores[n.Node] { |
| 87 | + parent[n.Node] = append(parent[n.Node], curr.Node) |
| 88 | + scores[n.Node] = n.Score |
| 89 | + heap.Push(pq, n) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + panic("no path found") |
| 94 | +} |
| 95 | + |
| 96 | +func neighbours(m map[P]struct{}, curr pqNode) []pqNode { |
| 97 | + list := []pqNode{ |
| 98 | + {Node{curr.Pos, curr.Direction.Clockwise()}, curr.Score + 1000}, |
| 99 | + {Node{curr.Pos, curr.Direction.Counterclockwise()}, curr.Score + 1000}, |
| 100 | + } |
| 101 | + |
| 102 | + delta := Delta[curr.Direction] |
| 103 | + if _, ok := m[P{curr.Pos.x + delta.x, curr.Pos.y + delta.y}]; ok { |
| 104 | + list = append(list, pqNode{Node{ |
| 105 | + Pos: P{curr.Pos.x + delta.x, curr.Pos.y + delta.y}, |
| 106 | + Direction: curr.Direction, |
| 107 | + }, curr.Score + 1}) |
| 108 | + } |
| 109 | + |
| 110 | + return list |
| 111 | +} |
0 commit comments