mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 14:50:46 +00:00
50 lines
1000 B
Go
50 lines
1000 B
Go
|
package two
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"io"
|
||
|
"regexp"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
reRegister = regexp.MustCompile(`Register (?P<identifier>[A-Z]): (?P<value>[0-9]+)`)
|
||
|
reProgram = regexp.MustCompile(`Program: (?P<instructions>[0-9,]+)`)
|
||
|
)
|
||
|
|
||
|
func parseLines(r io.Reader) (map[string]int64, []int64, error) {
|
||
|
registers := make(map[string]int64)
|
||
|
var instructions []int64
|
||
|
|
||
|
scanner := bufio.NewScanner(r)
|
||
|
for scanner.Scan() {
|
||
|
line := scanner.Text()
|
||
|
|
||
|
switch line := line; {
|
||
|
case reRegister.MatchString(line):
|
||
|
m := reRegister.FindStringSubmatch(line)
|
||
|
registers[m[1]] = mustConv(m[2])
|
||
|
case reProgram.MatchString(line):
|
||
|
m := reProgram.FindStringSubmatch(line)
|
||
|
for _, n := range strings.Split(m[1], ",") {
|
||
|
instructions = append(instructions, mustConv(n))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if err := scanner.Err(); err != nil {
|
||
|
return nil, nil, err
|
||
|
}
|
||
|
|
||
|
return registers, instructions, nil
|
||
|
}
|
||
|
|
||
|
func mustConv(s string) int64 {
|
||
|
n, err := strconv.Atoi(s)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return int64(n)
|
||
|
}
|