-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_1.go
67 lines (57 loc) · 977 Bytes
/
part_1.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func ScratchcardsWorth() {
file, err := os.Open("input.txt")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
var t int
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Split(line, ":")
if len(parts) < 2 {
continue
}
x := parts[1]
x = strings.TrimSpace(x)
arrays := strings.Split(x, " | ")
if len(arrays) != 2 {
continue
}
a := parseIntArray(arrays[0])
b := parseIntArray(arrays[1])
j := 0
for _, q := range b {
for _, value := range a {
if q == value {
j++
break
}
}
}
if j > 0 {
t += 1 << (j - 1)
}
}
fmt.Println(t)
}
func parseIntArray(s string) []int {
var result []int
parts := strings.Fields(s)
for _, part := range parts {
num, err := strconv.Atoi(part)
if err == nil {
result = append(result, num)
}
}
return result
}