-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08p1.go
55 lines (43 loc) · 1.12 KB
/
08p1.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
package main
import (
"fmt"
"aoc2023/utils"
)
type wasteLandLocation struct {
locationId string
moveLeft string
moveRight string
}
func D08P1() {
input := utils.ReadLines("inputs/08.txt")
moveList, locations := parseWasteland(input)
currentLocation := locations["AAA"]
moveIndex := 0
for currentLocation.locationId != "ZZZ" {
currentMove := moveList[moveIndex%len(moveList)]
if currentMove == 'L' {
currentLocation = locations[currentLocation.moveLeft]
}
if currentMove == 'R' {
currentLocation = locations[currentLocation.moveRight]
}
moveIndex++
}
fmt.Printf("Found ZZZ at %d moves\n", moveIndex)
}
func parseWasteland(input []string) ([]rune, map[string]wasteLandLocation) {
moveList := []rune{}
for _, move := range input[0] {
moveList = append(moveList, move)
}
locations := map[string]wasteLandLocation{}
for _, location := range input[2:] {
parsedLocation := wasteLandLocation{
locationId: string(location[0:3]),
moveLeft: string(location[7:10]),
moveRight: string(location[12:15]),
}
locations[parsedLocation.locationId] = parsedLocation
}
return moveList, locations
}