mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2026-01-02 03:07:47 +00:00
62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"regexp"
|
|
)
|
|
|
|
type M map[string]interface{}
|
|
|
|
var allCards []M
|
|
|
|
var _regex_winning_numbers = regexp.MustCompile(
|
|
`^Card (?P<card>[\s\d]+):\s` +
|
|
`(?P<winning>[0-9\s]+)\s` +
|
|
`\|\s` +
|
|
`(?P<mynums>[0-9\s]+)$`)
|
|
|
|
var _regex_nums = regexp.MustCompile("[0-9]+")
|
|
|
|
// compareLists returns the number of matching elements between winning and mynums
|
|
func compareLists(winningStr string, mynumsStr string) (int, error) {
|
|
x, err := numStrToArr(winningStr, []int{})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
winning := x
|
|
x, err = numStrToArr(mynumsStr, []int{})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
mynums := x
|
|
|
|
n := 0
|
|
for _, num := range mynums {
|
|
if contains(winning, num) {
|
|
n += 1
|
|
}
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// one computes points based on matching numbers
|
|
func one(lines []string) (int, error) {
|
|
for _, line := range lines {
|
|
parseAllCards(line)
|
|
}
|
|
cards = make([]Card, len(lines))
|
|
|
|
sum := 0
|
|
for x, card := range allCards {
|
|
m, err := compareLists(card["winning"].(string), card["mynums"].(string))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
cards[x].matches = m
|
|
if cards[x].matches > 0 {
|
|
sum += (pow(2, cards[x].matches-1))
|
|
}
|
|
}
|
|
|
|
return sum, nil
|
|
}
|