-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (73 loc) · 1.49 KB
/
main.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
package main
import (
"fmt"
"regexp"
"strings"
"utils"
)
type nodeMap struct {
Left string
Right string
}
func part2end(locations []string) bool {
for _, loc := range locations {
if loc[len(loc)-1] != 'Z' {
return false
}
}
return true
}
func countSteps(lr string, paths map[string][]string, loc string, part1 bool) int {
end := false
count := 0
for !end {
if lr[(count%len(lr))] == 'L' { // Left
loc = paths[loc][0]
} else { // Right
loc = paths[loc][1]
}
count++
if part1 {
end = loc == "ZZZ"
} else {
end = loc[len(loc)-1] == 'Z'
}
}
return count
}
func gcd(a int, b int) int { // Apply Euclidean algorithm
for b != 0 {
r := b
b = a % b
a = r
}
return a
}
func lcm(a int, b int) int {
return (a * b) / gcd(a, b)
}
func solve() {
sections := utils.ReadInput("input.txt", "\n\n")
lr := sections[0]
paths := make(map[string][]string)
re := regexp.MustCompile(`(.+) = \((.+), (.+)\)`)
startLocs := make([]string, 0)
for _, line := range strings.Split(sections[1], "\n") {
m := re.FindAllStringSubmatch(line, -1)
source := m[0][1]
paths[source] = []string{m[0][2], m[0][3]}
if source[len(source)-1] == 'A' { // Start location for part 2
startLocs = append(startLocs, source)
}
}
part1 := countSteps(lr, paths, "AAA", true)
part2 := 1
for _, start := range startLocs {
part2 = lcm(part2, countSteps(lr, paths, start, false))
}
fmt.Println("Part 1:", part1)
fmt.Println("Part 2:", part2)
}
func main() {
utils.TimeFunction(solve)
}