2023-12-04 17:23:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-04 18:45:30 +00:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
2023-12-04 17:23:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// one computes points based on matching numbers
|
|
|
|
func one(lines []string) (int, error) {
|
2023-12-04 18:45:30 +00:00
|
|
|
f := func(c rune) bool {
|
|
|
|
return !unicode.IsDigit(c)
|
2023-12-04 17:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sum := 0
|
2023-12-04 18:45:30 +00:00
|
|
|
cards = make([]Card, len(lines))
|
|
|
|
for x, line := range lines {
|
|
|
|
winning, mynums := func() (string, string) {
|
|
|
|
y := strings.Split(line, ":")
|
|
|
|
z := strings.Split(y[1], "|")
|
|
|
|
return z[0], z[1]
|
|
|
|
}()
|
|
|
|
|
|
|
|
m, err := compare(strings.FieldsFunc(winning, f), strings.FieldsFunc(mynums, f))
|
2023-12-04 17:23:02 +00:00
|
|
|
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
|
|
|
|
}
|