generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.go
50 lines (39 loc) · 742 Bytes
/
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
package day08p1
import (
"io"
"strings"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
// Get all LR moves
moves := []rune(lines[0])
neighbors := make(map[string][]string)
for i := 2; i < len(lines); i++ {
parts := strings.Split(lines[i], " = ")
destinations := strings.Trim(parts[1], "()")
leftright := strings.Split(destinations, ", ")
neighbors[parts[0]] = leftright
}
next := neighbors["AAA"]
steps := 0
outer:
for {
for _, d := range moves {
steps++
switch d {
case 'L':
if next[0] == "ZZZ" {
break outer
}
next = neighbors[next[0]]
case 'R':
if next[1] == "ZZZ" {
break outer
}
next = neighbors[next[1]]
}
}
}
return steps
}