package day8 import ( "fmt" "log" "os" "regexp" "slices" "strings" ) func Run() int { fmt.Print("hello day 8") filename := "day8/example3" net, path := Read(filename) fmt.Printf("got %+v and %+v\n", net, path) result := GhostTraverse(net, path) return result } func GhostTraverse(net Network, path Path) int { stepsNum := 0 simultaneousPaths := make([]Node, 0) for nodeName, node := range net.Nodes { if strings.HasSuffix(nodeName, "A") { simultaneousPaths = append(simultaneousPaths, node) } } log.Printf("collected start points: %+v\n", simultaneousPaths) // concurrent iteraction for !isGhostWayDone(simultaneousPaths) { direction := path.next() for i, curNode := range simultaneousPaths { simultaneousPaths[i] = getNextNode(net, curNode, direction) } // log.Printf("done step into %+v\n", simultaneousPaths) stepsNum += 1 } return stepsNum } func isGhostWayDone(simulaneousPath []Node) bool { containsNonZEnding := slices.ContainsFunc(simulaneousPath, func(curNode Node) bool { name := curNode.Name last := name[len(name) - 1] return last != 'Z' }) // log.Printf("checking if done for %+v : %t", simulaneousPath, !containsNonZEnding) return !containsNonZEnding } func getNextNode(net Network, curNode Node, direction rune) Node { var nextNodeName string switch direction { case 'L': nextNodeName = curNode.Left case 'R': nextNodeName = curNode.Right } nextNode, found := net.Nodes[nextNodeName] if !found { panic(fmt.Sprintf("instruction %s from node %+v results in not found next node %s", string(direction), curNode, nextNodeName)) } return nextNode } func TraverseNetwork(net Network, path Path) int { stepsNum := 0 curNode := net.Nodes["AAA"] for curNode.Name != "ZZZ" { direction := path.next() var nextNodeName string switch direction { case 'L': nextNodeName = curNode.Left case 'R': nextNodeName = curNode.Right } nextNode, found := net.Nodes[nextNodeName] if !found { panic(fmt.Sprintf("at step %d instruction %s from node %+v results in not found next node %s", stepsNum, string(direction), curNode, nextNodeName)) } curNode = nextNode stepsNum += 1 } return stepsNum } func Read(filename string) (Network, Path) { net := Network{ Nodes: make(map[string]Node), } bytes, err := os.ReadFile(filename) if err != nil { panic(fmt.Sprintln("error reading file ", filename)) } lines := strings.Split(string(bytes), "\n") path := Path{ Instruction: strings.TrimSpace(lines[0]), } netDescriptions := lines[2:] re := regexp.MustCompile(`(?P\D{3}) = \((?P\D{3}), (?P\D{3})\)`) nameIndex := re.SubexpIndex("NAME") leftIndex := re.SubexpIndex("LEFT") rightIndex := re.SubexpIndex("RIGHT") for _, line := range netDescriptions { if line == "" { continue } match := re.FindStringSubmatch(line) if match == nil { panic(fmt.Sprintln("error finding match in string : ", line)) } node := Node{ Name: match[nameIndex], Left: match[leftIndex], Right: match[rightIndex], } net.Nodes[node.Name] = node } return net, path } type Node struct { Name string Left, Right string } type Network struct { Nodes map[string]Node } type Path struct { Instruction string curStep int } func (p *Path) next() rune { curInstruction := p.Instruction[p.curStep] p.curStep += 1 if p.curStep == len(p.Instruction) { p.curStep = 0 } return rune(curInstruction) }