aoc2023/day-8/util.go

40 lines
720 B
Go
Raw Normal View History

2023-12-08 21:18:51 +00:00
package main
import (
"bufio"
"log"
"os"
"regexp"
)
// readlines reads lines from stdin.
// returns input as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
var r = regexp.MustCompile(`(?P<identifier>[A-Z0-9]{3}) = \((?P<entry>[A-Z0-9]{3}), (?P<exit>[A-Z0-9]{3})\)`)
func parselines(lines []string) {
directions = lines[0]
steps = make(map[string][]string, len(lines[2:]))
for _, line := range lines[2:] {
m := r.FindStringSubmatch(line)
if len(m) > 0 {
steps[m[1]] = []string{m[2], m[3]}
}
}
}