-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (57 loc) · 1.16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
_ "embed"
"fmt"
"math"
"regexp"
"slices"
"strconv"
"strings"
)
//go:embed input.txt
var input string
/**
* Day 4: Scratchcards - Part 1
* url: https://adventofcode.com/2023/day/4
*/
func main() {
lines := strings.Split(strings.TrimSpace(input), "\n")
cards := []int{}
for _, s := range lines {
win, got := parseLine(s)
points := countIntersection(win, got)
cards = append(cards, points)
}
sum := 0
for _, val := range cards {
if val > 0 {
sum += int(math.Pow(2, float64(val-1)))
}
}
fmt.Println("Part 1 = ", sum)
}
func parseLine(s string) ([]int, []int) {
startAt := strings.Index(strings.TrimSpace(s), ":")
sets := strings.Split(s[startAt+1:], "|")
win := parseNumbers(sets[0])
got := parseNumbers(sets[1])
return win, got
}
func parseNumbers(s string) []int {
re := regexp.MustCompile(`\d+`)
numbers := []int{}
for _, val := range re.FindAllStringSubmatch(s, -1) {
num, _ := strconv.Atoi(val[0])
numbers = append(numbers, num)
}
return numbers
}
func countIntersection(set1 []int, set2 []int) int {
count := 0
for _, val := range set1 {
if slices.Contains(set2, val) {
count++
}
}
return count
}