-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (67 loc) · 1.17 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
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"strings"
"github.com/devkvlt/aoc"
)
type Set = map[string]struct{}
func setFromSlice(s []string) Set {
set := make(Set)
for _, v := range s {
set[v] = struct{}{}
}
return set
}
func intersect(a, b Set) Set {
set := make(Set)
for e := range a {
if _, ok := b[e]; ok {
set[e] = struct{}{}
}
}
return set
}
func got(line string) int {
parts := strings.Split(strings.Split(line, ":")[1], "|")
have := setFromSlice(strings.Fields(parts[0]))
winning := setFromSlice(strings.Fields(parts[1]))
return len(intersect(have, winning))
}
func pow2(n int) int {
p := 1
for i := 0; i < n; i++ {
p *= 2
}
return p
}
func part1(lines []string) {
points := 0
for i := 0; i < len(lines); i++ {
got := got(lines[i])
if got != 0 {
points += pow2(got - 1)
}
}
fmt.Println(points)
}
func part2(lines []string) {
n := len(lines)
copies := make([]int, n)
sum := n
for i := 0; i < n; i++ {
copies[i]++
got := got(lines[i])
for j := 0; j < copies[i]; j++ {
for k := 1; k <= got && i+k < n; k++ {
copies[i+k]++
sum++
}
}
}
fmt.Println(sum)
}
func main() {
lines := aoc.Lines("input")
part1(lines)
part2(lines)
}