Skip to content

Commit b71e675

Browse files
committed
2024.16 Part 2 WIP
1 parent 8e6f5d0 commit b71e675

File tree

2 files changed

+79
-27
lines changed

2 files changed

+79
-27
lines changed

2024/16-olympics/example2.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#########
2+
#.#...###
3+
#...#..E#
4+
#S#....##
5+
#########

2024/16-olympics/main.go

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import (
99
)
1010

1111
const (
12-
bounds = '#'
13-
empty = '.'
14-
visited = '@'
15-
finish = 'E'
12+
bounds = '#'
13+
empty = '.'
14+
finish = 'E'
1615
)
1716

1817
var directions = map[byte]position{
@@ -24,16 +23,21 @@ var directions = map[byte]position{
2423

2524
var grid [][]byte
2625
var player path
26+
var visited [][]int
2727

2828
func main() {
2929
file := util.FileFromArgs()
3030
scan := bufio.NewScanner(file)
3131
for scan.Scan() {
32+
visited = append(visited, make([]int, len(scan.Bytes())))
3233
x := bytes.IndexRune(scan.Bytes(), 'S')
3334
if x > -1 {
3435
player.X = x
3536
player.Y = len(grid)
3637
player.Direction = 'E'
38+
player.History = []position{
39+
{player.X, player.Y},
40+
}
3741
}
3842

3943
gridRow := make([]byte, len(scan.Bytes()))
@@ -46,56 +50,98 @@ func main() {
4650
}
4751
heap.Init(paths)
4852

53+
var finishes []path
4954
var p path
5055
for {
56+
// All shortest paths are found, stop searching!
5157
if paths.Len() == 0 {
52-
util.PrintMatrix(grid)
53-
fmt.Println(finish)
54-
panic("No path found...")
58+
break
5559
}
5660

5761
p = heap.Pop(paths).(path)
5862
if grid[p.Y][p.X] == finish {
59-
fmt.Println(p.Score)
60-
break
63+
if len(finishes) == 0 || p.Score == finishes[0].Score {
64+
fmt.Println(p.Score) // Part 1
65+
finishes = append(finishes, p) // Part 2: Find all shortest routes
66+
}
67+
continue
68+
}
69+
70+
// Don't bother paths that are higher than the finish
71+
if p.Score > 109516 { // 109516 is the input answer for part 1 ;)
72+
continue
6173
}
6274

6375
var dx, dy int
76+
outer:
6477
for d, dd := range directions {
6578
dx, dy = p.X+dd.X, p.Y+dd.Y
66-
if grid[dy][dx] == bounds || grid[dy][dx] == visited {
79+
if grid[dy][dx] == bounds {
6780
continue
6881
}
69-
if grid[dy][dx] == empty {
70-
grid[dy][dx] = visited
82+
83+
// Have we been here before?
84+
for _, h := range p.History {
85+
if h.X == dx && h.Y == dy {
86+
continue outer
87+
}
7188
}
7289

73-
// Going straight on is free
74-
if p.Direction == d {
90+
// Have we got enough points to be here?
91+
if visited[dy][dx] > 0 && p.Score > visited[dy][dx] {
92+
continue
93+
}
94+
visited[dy][dx] = p.Score
95+
96+
if p.Direction == d { // Going straight on is free
7597
heap.Push(paths, path{
7698
X: dx,
7799
Y: dy,
78100
Score: p.Score + 1,
79101
Direction: d,
102+
History: makeHistory(p.History, position{dx, dy}),
103+
})
104+
} else { // Turning costs 1000
105+
heap.Push(paths, path{
106+
X: dx,
107+
Y: dy,
108+
Score: p.Score + 1001,
109+
Direction: d,
110+
History: makeHistory(p.History, position{dx, dy}),
80111
})
81-
82-
continue
83112
}
113+
}
114+
}
84115

85-
// No going backsies
86-
if directions[p.Direction].X+dd.X == 0 && directions[p.Direction].Y+dd.Y == 0 {
87-
continue
88-
}
116+
fmt.Println(finishes)
117+
118+
// Part 2: now find unique points
119+
var count int
89120

90-
// Turn here
91-
heap.Push(paths, path{
92-
X: dx,
93-
Y: dy,
94-
Score: p.Score + 1001,
95-
Direction: d,
96-
})
121+
for _, f := range finishes {
122+
for _, fh := range f.History {
123+
if grid[fh.Y][fh.X] != 'O' {
124+
count++
125+
grid[fh.Y][fh.X] = 'O'
126+
}
97127
}
98128
}
129+
130+
util.PrintMatrix(grid)
131+
fmt.Println(count)
132+
}
133+
134+
// makeHistory creates a brand new slice containing elements from h and p
135+
func makeHistory(h []position, p position) []position {
136+
newSlice := make([]position, len(h)+1)
137+
138+
var i int
139+
for i = range h {
140+
newSlice[i] = position{h[i].X, h[i].Y}
141+
}
142+
newSlice[i+1] = p
143+
144+
return newSlice
99145
}
100146

101147
type position struct {
@@ -106,6 +152,7 @@ type path struct {
106152
X, Y int
107153
Score int
108154
Direction byte
155+
History []position
109156
}
110157

111158
type pathHeap []path

0 commit comments

Comments
 (0)