-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (54 loc) · 1.04 KB
/
main.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
package main
import (
_ "embed"
"fmt"
"regexp"
"strings"
)
//go:embed input.txt
var input string
type Node struct {
name string
left string
right string
}
/**
* Day 8: Haunted Wasteland - Part 1
* url: https://adventofcode.com/2023/day/8
*/
func main() {
cmds, nodes := parseNodes()
steps := 0
current := "AAA"
for {
if current == "ZZZ" {
break
}
dir := cmds[steps%len(cmds)]
steps += 1
if dir == "L" {
current = nodes[current].left
} else {
current = nodes[current].right
}
}
fmt.Println("Part 1 =", steps)
}
func parseNodes() ([]string, map[string]*Node) {
lines := strings.Split(strings.TrimSpace(input), "\n")
cmds := strings.Split(strings.TrimSpace(lines[0]), "")
r := regexp.MustCompile(`(\w*) = \((\w*), (\w*)\)`)
mapNodes := make(map[string]*Node)
for _, s := range lines[1:] {
res := r.FindAllStringSubmatch(strings.TrimSpace(s), -1)
if len(res) == 0 {
continue
}
mapNodes[res[0][1]] = &Node{
name: res[0][1],
left: res[0][2],
right: res[0][3],
}
}
return cmds, mapNodes
}