generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
106 lines (88 loc) · 2.02 KB
/
solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package day10p1
import (
"io"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
maze := make(PipeMaze)
for j, ln := range lines {
for i, r := range ln {
p := utils.Point{X: i, Y: -j}
maze[p] = r
}
}
bfs := utils.NewBFS[utils.Point]()
_, err := bfs.Run(maze)
if err != utils.BFSNotFound {
utils.Check(err, "Error during search")
}
var max uint64
for _, v := range bfs.Distance {
if v > max {
max = v
}
}
return max
}
type PipeMaze map[utils.Point]rune
func (m PipeMaze) GetInitial() utils.Point {
for k, v := range m {
if v == 'S' {
return k
}
}
return utils.Point{}
}
func (m PipeMaze) GetNeighbors(p utils.Point) []utils.Point {
var directions []utils.Point
switch m[p] {
case '|':
directions = []utils.Point{utils.North, utils.South}
case '-':
directions = []utils.Point{utils.East, utils.West}
case 'L':
directions = []utils.Point{utils.North, utils.East}
case 'J':
directions = []utils.Point{utils.North, utils.West}
case '7':
directions = []utils.Point{utils.South, utils.West}
case 'F':
directions = []utils.Point{utils.South, utils.East}
case '.':
directions = []utils.Point{}
case 'S':
directions = []utils.Point{utils.North, utils.South, utils.East, utils.West}
}
// Validate connector and add if there is a connecting pipe
neighbors := []utils.Point{}
for _, d := range directions {
switch d {
case utils.North:
n := p.Add(d)
if m[n] == '|' || m[n] == '7' || m[n] == 'F' {
neighbors = append(neighbors, n)
}
case utils.South:
n := p.Add(d)
if m[n] == '|' || m[n] == 'L' || m[n] == 'J' {
neighbors = append(neighbors, n)
}
case utils.East:
n := p.Add(d)
if m[n] == '-' || m[n] == 'J' || m[n] == '7' {
neighbors = append(neighbors, n)
}
case utils.West:
n := p.Add(d)
if m[n] == '-' || m[n] == 'L' || m[n] == 'F' {
neighbors = append(neighbors, n)
}
}
}
return neighbors
}
func (m PipeMaze) IsFinal(p utils.Point) bool {
// Going to run until there are no more points
return false
}