mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-08 18:13:36 +00:00
add day-13 + benchmarks
This commit is contained in:
56
day-13/internal/util/util.go
Normal file
56
day-13/internal/util/util.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-13/internal/machine"
|
||||
)
|
||||
|
||||
var (
|
||||
reButton = regexp.MustCompile(`Button (?P<identifier>[A|B]): X\+(?P<xdigit>\d+), Y\+(?P<ydigit>\d+)`)
|
||||
rePrize = regexp.MustCompile(`Prize: X\=(?P<xdigit>\d+), Y\=(?P<ydigit>\d+)`)
|
||||
)
|
||||
|
||||
func ParseLines(r io.Reader) ([]*machine.Machine, error) {
|
||||
var matches [][]string
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
lineBytes := scanner.Bytes()
|
||||
if len(lineBytes) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
var m []string
|
||||
switch line := string(lineBytes); {
|
||||
case reButton.Match(lineBytes):
|
||||
m = reButton.FindStringSubmatch(line)
|
||||
case rePrize.Match(lineBytes):
|
||||
m = rePrize.FindStringSubmatch(line)
|
||||
}
|
||||
|
||||
matches = append(matches, m)
|
||||
}
|
||||
|
||||
var machines []*machine.Machine
|
||||
for i := 0; i < len(matches); i += 3 {
|
||||
machines = append(machines, machine.New(matches[i], matches[i+1], matches[i+2]))
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return machines, nil
|
||||
}
|
||||
|
||||
func mustConv(s string) int {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
Reference in New Issue
Block a user