-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
82 lines (64 loc) · 1.51 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
package main
import (
"fmt"
"io"
"strings"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
bags := toBags(lib.ReaderToStrings(input))
sum := 0
for bag := range bags {
if canContain(bags, bag, "shiny gold") {
sum++
}
}
return sum
}
func canContain(bags map[string]map[string]int, source, target string) bool {
bag := bags[source]
for inner := range bag {
if inner == target {
return true
} else {
if canContain(bags, inner, target) {
return true
}
}
}
return false
}
func toBags(lines []string) map[string]map[string]int {
bags := make(map[string]map[string]int)
for _, line := range lines {
line = line[:len(line)-1]
del := lib.NewDelimiter(line, " ")
bag := del.GetString(0) + " " + del.GetString(1)
bags[bag] = make(map[string]int)
if strings.Contains(line, "no other bags") {
continue
}
contain := "contain "
idx := strings.Index(line, contain) + len(contain)
s := line[idx:]
items := lib.NewDelimiter(s, ", ").GetStrings()
for _, item := range items {
words := lib.NewDelimiter(item, " ")
bags[bag][fmt.Sprintf("%s %s", words.GetString(1), words.GetString(2))] = words.GetInt(0)
}
}
return bags
}
func fs2(input io.Reader) int {
bags := toBags(lib.ReaderToStrings(input))
return sumInnerBags(bags, "shiny gold")
}
func sumInnerBags(bags map[string]map[string]int, source string) int {
bag := bags[source]
sum := 0
for inner, count := range bag {
sum += count
sum += count * sumInnerBags(bags, inner)
}
return sum
}