-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (73 loc) · 2.18 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
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"github.com/davidporos92/aoc-2020/utils"
"log"
"regexp"
"strconv"
"strings"
)
var bags = make(map[string]*Bag)
type Bag struct {
name string
canContain map[string]int
canBeContainedIn []string
}
func NewBag(name string) *Bag {
bag := &Bag{
name: name,
canContain: make(map[string]int),
canBeContainedIn: make([]string, 0),
}
bags[name] = bag
return bag
}
func main() {
ruleMatcher := regexp.MustCompile(`(\w+\s\w+)\sbags?\scontain\s(.*)`)
canContainMatcher := regexp.MustCompile(`(\d+)\s(\w+\s\w+)\sbags?`)
myBagName := "shiny gold"
utils.NewReader("./input-1.dat").MustReadFile(func(rule string) {
ruleMatches := ruleMatcher.FindStringSubmatch(rule)
ruleBag, exist := bags[ruleMatches[1]]
if !exist {
ruleBag = NewBag(ruleMatches[1])
}
if strings.Contains(ruleMatches[2], "no other bag") {
return
}
ruleBagCanContain := strings.Split(ruleMatches[2], ",")
for _, canContainRule := range ruleBagCanContain {
canContainRule = strings.Trim(canContainRule, " ")
canBeContainedInBagRule := canContainMatcher.FindStringSubmatch(canContainRule)
qty, err := strconv.Atoi(canBeContainedInBagRule[1])
if err != nil {
log.Fatalf("Cannot convert %s to int: %v", canBeContainedInBagRule[1], err)
}
ruleBag.canContain[canBeContainedInBagRule[2]] = qty
canBeContainedInBag, exists := bags[canBeContainedInBagRule[2]]
if !exists {
canBeContainedInBag = NewBag(canBeContainedInBagRule[2])
}
canBeContainedInBag.canBeContainedIn = append(canBeContainedInBag.canBeContainedIn, ruleBag.name)
}
})
myBag := bags[myBagName]
fmt.Printf("Solution 1: %d\n", BagsCanHold(myBag.name))
fmt.Printf("Solution 2: %d\n", NumberOfBagsMustBeContained(myBag))
}
func BagsCanHold(bagName string) int {
count := 0
for _, bag := range bags {
if _, exists := bag.canContain[bagName]; exists {
count += 1 + BagsCanHold(bag.name)
}
}
return count
}
func NumberOfBagsMustBeContained(bag *Bag) int {
count := 0
for containedBag, containedBagCount := range bag.canContain {
count += containedBagCount + (containedBagCount * NumberOfBagsMustBeContained(bags[containedBag]))
}
return count
}