10 "adventofcode2023/utils"
12 mapset "github.com/deckarep/golang-set/v2"
19 winners mapset.Set[int]
23 func Part1(input string) int {
24 games, err := parseGames(input)
26 fmt.Println("Error:", err)
31 for _, game := range games {
32 matches := len(game.cards.Intersect(game.winners).ToSlice())
34 points += math.Pow(2, float64(matches - 1))
40 func Part2(input string) int {
41 games, err := parseGames(input)
43 fmt.Println("Error:", err)
47 for i, game := range games {
48 matches := game.cards.Intersect(game.winners).ToSlice()
49 games[i].matches = len(matches)
53 for i:=0;i<len(games);i++{
54 for j:=0;j<games[i].count;j++ {
55 for k:=1;k<=games[i].matches;k++ {
62 for i:=0;i<len(games);i++{
63 cards += games[i].count
68 func parseGames(input string) ([]Game, error) {
73 lines := strings.Split(input, "\n")
74 for _, line := range lines {
75 reID := regexp.MustCompile(`Card\s*(\d+):(.*) \| (.*)`)
76 match := reID.FindStringSubmatch(line)
78 return games, fmt.Errorf("unable to extract game")
81 game.ID = utils.MustAtoi(match[1])
82 game.winners = getNumbers(match[2])
83 game.cards = getNumbers(match[3])
85 games = append(games, game)
90 func getNumbers(input string) mapset.Set[int] {
91 tokens := strings.Fields(input)
93 for _, token := range tokens {
94 num := utils.MustAtoi(token)
95 numbers = append(numbers, num)
97 return mapset.NewSet[int](numbers...)