-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.go
102 lines (86 loc) · 1.91 KB
/
day08.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
package main
import (
_ "embed"
"fmt"
"strings"
)
//go:embed input.txt
var input string
func parseInstructions(line string) []int {
instructions := make([]int, len(line))
for i := 0; i < len(line); i++ {
if line[i] == 'L' {
instructions[i] = 0
} else {
instructions[i] = 1
}
}
return instructions
}
func lettersToIndex(s string) int {
return int(s[0]-'A')*26*26 + int(s[1]-'A')*26 + int(s[2]-'A')
}
func parseNetwork(lines []string) ([][2]int, []int) {
netwrok := make([][2]int, 26*26*26)
var endWithA []int
for _, line := range lines {
index := lettersToIndex(line[:3])
netwrok[index] = [2]int{
lettersToIndex(line[7:10]),
lettersToIndex(line[12:15]),
}
if line[2] == 'A' {
endWithA = append(endWithA, index)
}
}
return netwrok, endWithA
}
func GCD(a, b int) int {
for b != 0 {
t := b
b = a % b
a = t
}
return a
}
func LCM(n []int) int {
result := n[0] * n[1] / GCD(n[0], n[1])
for i := 2; i < len(n); i++ {
result = LCM([]int{result, n[i]})
}
return result
}
func part1(input string) int {
lines := strings.Split(input, "\n")
instructions := parseInstructions(lines[0])
network, _ := parseNetwork(lines[2:])
current := 0
var step int
for current != lettersToIndex("ZZZ") {
instructionIndex := step % len(instructions)
current = network[current][instructions[instructionIndex]]
step++
}
return step
}
func part2(input string) int {
lines := strings.Split(input, "\n")
instructions := parseInstructions(lines[0])
network, starts := parseNetwork(lines[2:])
var steps []int
for _, current := range starts {
var step int
for (current+1)%26 != 0 {
instructionIndex := step % len(instructions)
current = network[current][instructions[instructionIndex]]
step++
}
steps = append(steps, step)
}
return LCM(steps)
}
func main() {
fmt.Println("--- 2023 day 08 answer ---")
fmt.Println("part 1:\t", part1(input))
fmt.Println("part 2:\t", part2(input))
}