-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (69 loc) · 1.36 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
85
86
87
88
89
90
package main
import (
"fmt"
"strings"
"github.com/devkvlt/aoc"
)
var lines = aoc.Lines("input")
func parseNodes() (map[string][2]string, []string) {
nodes := map[string][2]string{}
startingNodes := []string{}
for _, line := range lines[2:] {
nodes[line[:3]] = [2]string{line[7:10], line[12:15]}
if strings.HasSuffix(line[:3], "A") {
startingNodes = append(startingNodes, line[:3])
}
}
return nodes, startingNodes
}
func part1() {
nodes, _ := parseNodes()
steps := 0
current := "AAA"
for current != "ZZZ" {
for _, direction := range lines[0] {
if direction == 'L' {
current = nodes[current][0]
} else {
current = nodes[current][1]
}
steps++
}
}
fmt.Println(steps)
}
func gcd(a, b int) int {
for b != 0 {
a, b = b, a%b
}
return a
}
func lcm(nums []int) int {
lcm := 1
for i := 0; i < len(nums); i++ {
lcm *= nums[i] / gcd(lcm, nums[i])
}
return lcm
}
func part2() {
nodes, startingNodes := parseNodes()
stepsList := make([]int, len(startingNodes))
for i := 0; i < len(startingNodes); i++ {
current := startingNodes[i]
for !strings.HasSuffix(current, "Z") {
for _, direction := range lines[0] {
if direction == 'L' {
current = nodes[current][0]
} else {
current = nodes[current][1]
}
stepsList[i]++
}
}
}
fmt.Println(lcm(stepsList))
}
func main() {
part1()
part2()
}