github.com/agrigoryan/aoc_2023_go@v0.0.0-20231216221323-4ace361ec685/day8/d8p1.go (about) 1 package day8 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 type node struct { 9 name string 10 left string 11 right string 12 endingNode bool 13 } 14 15 func d8p1(input string) int { 16 lines := strings.Split(input, "\n") 17 18 directions := []rune(lines[0]) 19 20 nodesByName := map[string]*node{} 21 22 for _, line := range lines[2:] { 23 name := strings.Split(line, " ")[0] 24 left := line[strings.Index(line, "(")+1 : strings.Index(line, ",")] 25 right := line[strings.Index(line, ", ")+2 : strings.Index(line, ")")] 26 nodesByName[name] = &node{name, left, right, false} 27 } 28 29 steps := 0 30 destination := "ZZZ" 31 currNode := nodesByName["AAA"] 32 for { 33 if currNode.name == destination { 34 break 35 } 36 direction := directions[steps%len(directions)] 37 if direction == 'R' { 38 currNode = nodesByName[currNode.right] 39 } else { 40 currNode = nodesByName[currNode.left] 41 } 42 steps += 1 43 } 44 45 fmt.Println(steps) 46 return steps 47 }