From f0c7f9815e620e0be5fbbf5a87f490bdf477c32a Mon Sep 17 00:00:00 2001 From: efim Date: Fri, 8 Dec 2023 06:57:24 +0000 Subject: [PATCH] day8, example --- day8/dayEight.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++ day8/example1 | 9 +++++ day8/example2 | 5 +++ main.go | 7 ++-- 4 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 day8/dayEight.go create mode 100644 day8/example1 create mode 100644 day8/example2 diff --git a/day8/dayEight.go b/day8/dayEight.go new file mode 100644 index 0000000..15d2a47 --- /dev/null +++ b/day8/dayEight.go @@ -0,0 +1,102 @@ +package day8 + +import ( + "fmt" + "os" + "regexp" + "strings" +) + +func Run() int { + fmt.Print("hello day 8") + filename := "day8/example1" + net, path := Read(filename) + fmt.Printf("got %+v and %+v\n",net, path) + result := TraverseNetwork(net, path) + return result +} + +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) +} diff --git a/day8/example1 b/day8/example1 new file mode 100644 index 0000000..9029a1b --- /dev/null +++ b/day8/example1 @@ -0,0 +1,9 @@ +RL + +AAA = (BBB, CCC) +BBB = (DDD, EEE) +CCC = (ZZZ, GGG) +DDD = (DDD, DDD) +EEE = (EEE, EEE) +GGG = (GGG, GGG) +ZZZ = (ZZZ, ZZZ) diff --git a/day8/example2 b/day8/example2 new file mode 100644 index 0000000..7d1b58d --- /dev/null +++ b/day8/example2 @@ -0,0 +1,5 @@ +LLR + +AAA = (BBB, BBB) +BBB = (AAA, ZZZ) +ZZZ = (ZZZ, ZZZ) diff --git a/main.go b/main.go index a561580..3bdcc98 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,12 @@ package main import ( "log" - "sunshine.industries/aoc2023/day7" + "sunshine.industries/aoc2023/day8" ) func main() { log.Print("> starting run:") - result := day7.Run() - log.Printf("day7 result: %d\n****\n", result) + result := day8.Run() + log.Printf("day8 result: %d\n****\n", result) + }