|
|
|
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"
|
|
|
|
}
|