solutions for the Advent of Code 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.1 KiB

package main
import (
"bufio"
"bytes"
"fmt"
"io"
"unicode"
)
type nextlocation struct {
left string
right string
}
func readsteps(b *bufio.Reader) []byte {
steps, err := b.ReadBytes('\n')
must(err)
return bytes.TrimFunc(steps, unicode.IsSpace)
}
type day08 struct{}
func (d day08) solve1(r io.Reader) string {
iterations := 0
current := "AAA"
buf := bufio.NewReader(r)
steps := readsteps(buf)
_, err := buf.ReadBytes('\n')
must(err)
m := make(map[string]nextlocation)
for next := true; next; {
line, err := buf.ReadBytes('(')
must(err)
location := string(line[:3])
left, err := buf.ReadBytes(',')
must(err)
remaining, err := buf.ReadBytes('\n')
next = !reachedeof(err)
nextlocation := nextlocation{
left: string(bytes.Trim(left, ",")),
right: string(remaining[1:4]),
}
m[location] = nextlocation
}
for ; current != "ZZZ"; iterations++ {
step := steps[iterations%len(steps)]
if step == 'R' {
current = m[current].right
} else {
current = m[current].left
}
}
return fmt.Sprintf("%d", iterations)
}
func (d day08) solve2(r io.Reader) string {
return "incomplete"
}