-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday08.go
More file actions
executable file
·201 lines (162 loc) · 4.49 KB
/
day08.go
File metadata and controls
executable file
·201 lines (162 loc) · 4.49 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"AdventOfCode-go/advent2023/utils"
"fmt"
"strings"
)
// File: First line is the movement instructions. Collection of L or R letters to signify movement
// Rest of the file contains direction nodes which consist of a left element and a right element (BBB, CCC)
// When running the movement phase the L or R command tells you which element to pick from where you are located
type locNode struct {
left string
right string
}
func day08b(filename string, part byte, debug bool) int {
locIndex := make(map[string]locNode)
var currLoc []string
puzzleInput, _ := utils.ReadFile(filename)
var movements string
for line, puzzleLine := range puzzleInput {
if line == 0 {
movements = puzzleLine
continue
}
if len(puzzleLine) == 0 {
continue
}
var location, left, right string
fields := strings.Fields(puzzleLine)
location = fields[0]
left = fields[2][1:4]
right = fields[3][0:3]
locIndex[location] = locNode{left, right}
if strings.HasSuffix(location, "A") {
currLoc = append(currLoc, location)
}
}
if debug {
fmt.Println("movements:", movements)
fmt.Println("LocIndex:", locIndex)
fmt.Println("currLoc:", currLoc)
}
if debug {
fmt.Println("All starting positions:", currLoc)
}
var currStep int
ghostResults := make([]int, len(currLoc))
// Although the puzzle says we should move all at the same time, we're not going to do that
// Loops through each of the starting points, looking for when that start reaches a "Z" end point
// Once we get to the end, record the number of steps it took to get there
for ghostIndex, ghostLoc := range currLoc {
currStep = 0
for while := false; !while; {
currMove := movements[currStep%len(movements)]
if debug {
fmt.Println("currMove:", currMove)
}
switch currMove {
case 'L':
ghostLoc = locIndex[ghostLoc].left
case 'R':
ghostLoc = locIndex[ghostLoc].right
default:
panic("Bad move in the movements list")
}
if strings.HasSuffix(ghostLoc, "Z") {
// this one is done. Record it's position for later LCM use
while = true
}
currStep++
}
ghostResults[ghostIndex] = currStep
}
// Now we have the number of steps needed for each of the starting positions to reach the end
// position, we use Least Common Multiplier to work out the point at which the results for all converge
var totalResult int64
totalResult = int64(ghostResults[0])
for i := 1; i < len(ghostResults); i++ {
totalResult = LCM(totalResult, int64(ghostResults[i]))
}
return int(totalResult)
}
//GCD greatest common divisor (GCD) via Euclidean algorithm
// Code lifted from Go Playground: https://play.golang.org/p/SmzvkDjYlb
func GCD(a, b int64) int64 {
for b != 0 {
t := b
b = a % b
a = t
}
return a
}
//LCM find Least Common Multiple (LCM) via GCD
// Code lifted from Go Playground: https://play.golang.org/p/SmzvkDjYlb
func LCM(a, b int64, integers ...int64) int64 {
result := a * b / GCD(a, b)
for i := 0; i < len(integers); i++ {
result = LCM(result, integers[i])
}
return result
}
func day08a(filename string, part byte, debug bool) int {
locIndex := make(map[string]locNode)
puzzleInput, _ := utils.ReadFile(filename)
var movements string
for line, puzzleLine := range puzzleInput {
if line == 0 {
movements = puzzleLine
continue
}
if len(puzzleLine) == 0 {
continue
}
var location, left, right string
fields := strings.Fields(puzzleLine)
location = fields[0]
left = fields[2][1:4]
right = fields[3][0:3]
locIndex[location] = locNode{left, right}
if debug {
fmt.Printf("location: %s left: %s right: %s map: %v\n", location, left, right, locIndex[location])
}
}
if debug {
fmt.Println(movements)
fmt.Println(locIndex)
}
var currStep int
var currLoc string = "AAA"
var totalSteps int
for while := false; !while; {
currMove := movements[currStep%len(movements)]
if debug {
fmt.Println("currLoc:", currLoc)
}
switch currMove {
case 'L':
currLoc = locIndex[currLoc].left
case 'R':
currLoc = locIndex[currLoc].right
default:
panic("Bad move in the movements list")
}
totalSteps++
if currLoc == "ZZZ" {
while = true
}
currStep++
}
return totalSteps
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
switch execPart {
case 'a':
fmt.Printf("Result is: %d\n", day08a(filenamePtr, execPart, debug))
case 'b':
fmt.Printf("Result is: %d\n", day08b(filenamePtr, execPart, debug))
default:
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
}
}