-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10p1.go
119 lines (102 loc) · 2.2 KB
/
10p1.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
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"fmt"
"aoc2023/utils"
)
type pipe struct {
connectionType rune
connectsTo []coordinate
distanceFromStart int
visited bool
}
func D10P1() {
lines := utils.ReadLines("inputs/10.txt")
pipeMap, start := parsePipes(lines)
if startingPipe, ok := pipeMap[start]; ok {
startingPipe.distanceFromStart = 0
pipeMap[start] = startingPipe
}
travelThroughPipes(pipeMap, start)
maxDistance := 0
for _, value := range pipeMap {
if value.distanceFromStart > maxDistance {
maxDistance = value.distanceFromStart
}
}
fmt.Printf("Max distance: %d\n", maxDistance)
}
func parsePipes(lines []string) (map[coordinate]pipe, coordinate) {
pipeMap := make(map[coordinate]pipe)
start := coordinate{}
for y, line := range lines {
for x, char := range line {
switch char {
case '|', '-', 'S', 'F', '7', 'J', 'L':
pipeMap[coordinate{x, y}] = pipe{
connectionType: char,
connectsTo: findConnections(coordinate{x, y}, char),
visited: false,
}
if char == 'S' {
start = coordinate{x, y}
}
}
}
}
return pipeMap, start
}
func findConnections(c coordinate, char rune) []coordinate {
switch char {
case '|', 'S':
return []coordinate{
{c.x, c.y - 1},
{c.x, c.y + 1},
}
case '-':
return []coordinate{
{c.x - 1, c.y},
{c.x + 1, c.y},
}
case 'F':
return []coordinate{
{c.x, c.y + 1},
{c.x + 1, c.y},
}
case '7':
return []coordinate{
{c.x, c.y + 1},
{c.x - 1, c.y},
}
case 'J':
return []coordinate{
{c.x, c.y - 1},
{c.x - 1, c.y},
}
case 'L':
return []coordinate{
{c.x, c.y - 1},
{c.x + 1, c.y},
}
}
return []coordinate{}
}
func travelThroughPipes(pipeMap map[coordinate]pipe, start coordinate) {
currentPipe := pipeMap[start]
currentPipe.visited = true
pipeMap[start] = currentPipe
queue := []pipe{currentPipe}
for len(queue) > 0 {
currentPipe = queue[0]
queue = queue[1:]
for _, connection := range currentPipe.connectsTo {
if pipe, ok := pipeMap[connection]; ok {
if !pipe.visited {
pipe.distanceFromStart = currentPipe.distanceFromStart + 1
pipe.visited = true
pipeMap[connection] = pipe
queue = append(queue, pipe)
}
}
}
}
}