-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (96 loc) · 2.37 KB
/
main.go
File metadata and controls
125 lines (96 loc) · 2.37 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"regexp"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "7", "input.txt"))
if err != nil {
panic(err)
}
return input
}
const (
targetBag = "shiny gold"
noContents = "no other bags"
)
var (
innerBag = regexp.MustCompile(`(\d+?) (.+?) bags?`)
ruleRegex = regexp.MustCompile(`^(.+?) bags contain (.+?)\.$`)
)
func parse(r io.Reader) map[string]map[string]bool {
scanner := bufio.NewScanner(r)
rules := make(map[string]map[string]bool)
for scanner.Scan() {
row := scanner.Text()
outer, inner := parseRule(row)
rules[outer] = inner
}
if scanner.Err() != nil {
panic(scanner.Err())
}
return rules
}
func parseRule(raw string) (string, map[string]bool) {
ruleMatches := ruleRegex.FindStringSubmatch(raw)
if ruleMatches == nil {
panic(fmt.Sprintf("don't know how to parse rule %s", raw))
}
outer := ruleMatches[1]
if ruleMatches[2] == noContents {
return outer, nil
}
inner := make(map[string]bool)
rawContents := strings.Split(ruleMatches[2], ",")
for _, rawBag := range rawContents {
innerBagMatches := innerBag.FindStringSubmatch(rawBag)
if innerBagMatches == nil {
panic(fmt.Sprintf("don't know how to parse inner bag %s", rawBag))
}
inner[innerBagMatches[2]] = true
}
return outer, inner
}
func solve(rules map[string]map[string]bool) int {
sum := 0
cache := make(map[string]bool)
for color := range rules {
if containsTargetBag(color, cache, rules) {
sum += 1
}
}
return sum
}
func containsTargetBag(color string, cache map[string]bool, rules map[string]map[string]bool) bool {
if containsBag, ok := cache[color]; ok {
return containsBag
}
result := func(result bool) bool {
cache[color] = result
return result
}
// search direct contents
for newColor := range rules[color] {
if newColor == targetBag {
return result(true)
}
}
// search indirect contents
for newColor := range rules[color] {
if containsTargetBag(newColor, cache, rules) {
return result(true)
}
}
return result(false)
}
func main() {
fmt.Println(parseRule("muted white bags contain 3 muted tomato bags, 5 light black bags, 4 pale black bags, 5 shiny gold bags."))
fmt.Println(parseRule("light black bags contain 1 striped yellow bag."))
fmt.Println(parseRule("dotted tomato bags contain no other bags."))
fmt.Println(solve(parse(input())))
}