-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08p2.go
56 lines (45 loc) · 1.28 KB
/
08p2.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
package main
import (
"fmt"
"aoc2023/utils"
)
func D08P2() {
input := utils.ReadLines("inputs/08.txt")
moveList, locations := parseWasteland(input)
currentLocations := findStartingWaselandLocations(locations)
paths := []int{}
for _, location := range currentLocations {
shortestPath := findWasteLandPathDistance(location, locations, moveList)
paths = append(paths, shortestPath)
}
lcm := utils.FindLowestCommonMultiple(paths)
fmt.Printf("Found wasteland end after %d moves\n", lcm)
}
func findStartingWaselandLocations(locations map[string]wasteLandLocation) []wasteLandLocation {
startingLocations := []wasteLandLocation{}
for _, location := range locations {
if location.locationId[2] == 'A' {
startingLocations = append(startingLocations, location)
}
}
return startingLocations
}
func findWasteLandPathDistance(
startingLocation wasteLandLocation,
locations map[string]wasteLandLocation,
moveList []rune,
) int {
currentLocation := startingLocation
moveIndex := 0
for currentLocation.locationId[2] != 'Z' {
currentMove := moveList[moveIndex%len(moveList)]
if currentMove == 'L' {
currentLocation = locations[currentLocation.moveLeft]
}
if currentMove == 'R' {
currentLocation = locations[currentLocation.moveRight]
}
moveIndex++
}
return moveIndex
}