day8, example3
This commit is contained in:
parent
414757f3ea
commit
dea9d15c66
|
@ -2,20 +2,71 @@ package day8
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run() int {
|
func Run() int {
|
||||||
fmt.Print("hello day 8")
|
fmt.Print("hello day 8")
|
||||||
filename := "day8/input"
|
filename := "day8/example3"
|
||||||
net, path := Read(filename)
|
net, path := Read(filename)
|
||||||
fmt.Printf("got %+v and %+v\n", net, path)
|
fmt.Printf("got %+v and %+v\n", net, path)
|
||||||
result := TraverseNetwork(net, path)
|
result := GhostTraverse(net, path)
|
||||||
return result
|
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 {
|
func TraverseNetwork(net Network, path Path) int {
|
||||||
stepsNum := 0
|
stepsNum := 0
|
||||||
|
|
||||||
|
@ -92,6 +143,7 @@ type Path struct {
|
||||||
Instruction string
|
Instruction string
|
||||||
curStep int
|
curStep int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Path) next() rune {
|
func (p *Path) next() rune {
|
||||||
curInstruction := p.Instruction[p.curStep]
|
curInstruction := p.Instruction[p.curStep]
|
||||||
p.curStep += 1
|
p.curStep += 1
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
LR
|
||||||
|
|
||||||
|
GGA = (GGB, XXX)
|
||||||
|
GGB = (XXX, GGZ)
|
||||||
|
GGZ = (GGB, XXX)
|
||||||
|
HHA = (HHB, XXX)
|
||||||
|
HHB = (HHC, HHC)
|
||||||
|
HHC = (HHZ, HHZ)
|
||||||
|
HHZ = (HHB, HHB)
|
||||||
|
XXX = (XXX, XXX)
|
Loading…
Reference in New Issue