Days 7,8,9
This commit is contained in:
99
2023/gareth/day08/day08.go
Normal file
99
2023/gareth/day08/day08.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package day08
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Direction struct {
|
||||
Destination string
|
||||
Left string
|
||||
Right string
|
||||
}
|
||||
|
||||
func Part1(input string) int {
|
||||
lines := strings.Split(input, "\r\n")
|
||||
dir := make(map[string]Direction)
|
||||
for _, line := range lines {
|
||||
pattern := `[A-Z]+`
|
||||
re := regexp.MustCompile(pattern)
|
||||
matches := re.FindAllString(line, -1)
|
||||
dir[matches[0]] = Direction{matches[0], matches[1], matches[2]}
|
||||
}
|
||||
|
||||
currentloc := "AAA"
|
||||
m := "LRLRRLLRRLRRRLRLRRRLLRRLLLLRRRLRRRLRRLRRLRRRLRRRLLRRLRLRRLRRRLLLRRLRRLLRLLRRRLRRRLLRLRRRLRLLRRLLLRLRRRLRRRLRRRLLRLRRRLLRRLRLRLLRRLRRRLRRLRLLRLRRRLRRLRLRLRRLRRRLRRRLRRRLRRLRRRLLRRLRRLLRRRLLRLRLRLRLLLRRLRLRRLRRLRRLRRLRRRLRRRLRLRRRLRLRRRLRRLRLLRLRRLRLRLLLRLLLRRRLRRLLLRLRRRR"
|
||||
moves := strings.Split(m, "")
|
||||
moveIndex := 0
|
||||
total := 0
|
||||
for {
|
||||
move := moves[moveIndex]
|
||||
if move == "R" {
|
||||
currentloc = dir[currentloc].Right
|
||||
} else {
|
||||
currentloc = dir[currentloc].Left
|
||||
}
|
||||
total++
|
||||
if currentloc == "ZZZ" {
|
||||
return total
|
||||
}
|
||||
moveIndex = int(math.Mod(float64(moveIndex+1), float64(len(moves))))
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
lines := strings.Split(input, "\r\n")
|
||||
dir := make(map[string]Direction)
|
||||
for _, line := range lines {
|
||||
pattern := `[A-Z1-9]+`
|
||||
re := regexp.MustCompile(pattern)
|
||||
matches := re.FindAllString(line, -1)
|
||||
dir[matches[0]] = Direction{matches[0], matches[1], matches[2]}
|
||||
}
|
||||
var startingLoc []string
|
||||
for _, loc := range dir {
|
||||
des := loc.Destination
|
||||
if des[2] == 'A' {
|
||||
startingLoc = append(startingLoc, des)
|
||||
}
|
||||
}
|
||||
|
||||
m := "LRLRRLLRRLRRRLRLRRRLLRRLLLLRRRLRRRLRRLRRLRRRLRRRLLRRLRLRRLRRRLLLRRLRRLLRLLRRRLRRRLLRLRRRLRLLRRLLLRLRRRLRRRLRRRLLRLRRRLLRRLRLRLLRRLRRRLRRLRLLRLRRRLRRLRLRLRRLRRRLRRRLRRRLRRLRRRLLRRLRRLLRRRLLRLRLRLRLLLRRLRLRRLRRLRRLRRLRRRLRRRLRLRRRLRLRRRLRRLRLLRLRRLRLRLLLRLLLRRRLRRLLLRLRRRR"
|
||||
moves := strings.Split(m, "")
|
||||
moveIndex := 0
|
||||
startingLocIndex := 0
|
||||
currentloc := ""
|
||||
total := 0
|
||||
for {
|
||||
for _, loc := range startingLoc {
|
||||
currentloc = loc
|
||||
move := moves[moveIndex]
|
||||
if move == "R" {
|
||||
currentloc = dir[currentloc].Right
|
||||
} else {
|
||||
currentloc = dir[currentloc].Left
|
||||
}
|
||||
startingLoc[startingLocIndex] = currentloc
|
||||
startingLocIndex = int(math.Mod(float64(startingLocIndex+1), float64(len(startingLoc))))
|
||||
}
|
||||
total++
|
||||
endFlag := false
|
||||
for _, loc := range startingLoc {
|
||||
if loc[2] == 'Z' {
|
||||
endFlag = true
|
||||
} else {
|
||||
endFlag = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if endFlag == true {
|
||||
return total
|
||||
}
|
||||
moveIndex = int(math.Mod(float64(moveIndex+1), float64(len(moves))))
|
||||
fmt.Println(total)
|
||||
}
|
||||
return -2
|
||||
}
|
||||
Reference in New Issue
Block a user