mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
40 lines
720 B
Go
40 lines
720 B
Go
|
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]}
|
||
|
}
|
||
|
}
|
||
|
}
|