Skip to content

Commit 0f9a55d

Browse files
authored
Merge pull request #68 from tholok97/day7
Day 7 in Go
2 parents f631be4 + 492fe52 commit 0f9a55d

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

days/day-07/solutions/tholok97.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"regexp"
8+
"strconv"
9+
)
10+
11+
type pair struct {
12+
amount int
13+
color string
14+
}
15+
16+
func main() {
17+
scanner := bufio.NewScanner(os.Stdin)
18+
19+
rules := make(map[string][]pair)
20+
21+
for scanner.Scan() {
22+
ruleString := scanner.Text()
23+
color, contained := parseRule(ruleString)
24+
rules[color] = contained
25+
}
26+
27+
sum := 0
28+
29+
for key := range rules {
30+
if contains(rules, key, "shiny gold") {
31+
sum++
32+
}
33+
}
34+
35+
fmt.Println(sum)
36+
37+
// subtract one cost as we care about the cost of the bags **within** the
38+
// initial bag
39+
fmt.Println(colorCost(rules, "shiny gold") - 1)
40+
}
41+
42+
func parseRule(s string) (color string, contained []pair) {
43+
reBagColor := regexp.MustCompile(`^([\w ]*) bags contain`)
44+
reBagColorMatches := reBagColor.FindStringSubmatch(s)
45+
color = reBagColorMatches[1]
46+
reNoOther := regexp.MustCompile(`no other bags`)
47+
48+
if reNoOther.MatchString(s) {
49+
// No bags are contained, return early
50+
return color, make([]pair, 0)
51+
}
52+
53+
reBagContains := regexp.MustCompile(`(,? (\d+) ([\w ]*) bags?)`)
54+
reBagContainsMatches := reBagContains.FindAllStringSubmatch(s, -1)
55+
contained = make([]pair, len(reBagContainsMatches))
56+
57+
for i, matches := range reBagContainsMatches {
58+
amount, err := strconv.Atoi(matches[2])
59+
60+
if err != nil {
61+
panic("misbehaving number string >:(")
62+
}
63+
64+
contained[i] = pair{
65+
amount: amount, // don't care about this one
66+
color: matches[3],
67+
}
68+
}
69+
70+
return color, contained
71+
}
72+
73+
// does 'color' contain 'target'?
74+
// assume X can't contain X
75+
func contains(rules map[string][]pair, color, target string) bool {
76+
for _, contained := range rules[color] {
77+
if contained.color == target {
78+
return true
79+
}
80+
81+
if contains(rules, contained.color, target) {
82+
return true
83+
}
84+
}
85+
return false
86+
}
87+
88+
// what's the total cost of 'color'?
89+
func colorCost(rules map[string][]pair, color string) int {
90+
// myself
91+
cost := 1
92+
93+
for _, contained := range rules[color] {
94+
// + the cost of each of my bags
95+
cost += contained.amount * colorCost(rules, contained.color)
96+
}
97+
98+
return cost
99+
}

days/day-07/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ D=$(dirname $(realpath $0))
77
echo ""
88
echo "--- Day 7: Handy Haversacks ---"
99
$D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day07.sml
10+
$D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/tholok97.go
1011
$D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day07.stektpotet.py
1112
$D/../../languages/node.sh $D/input.txt $D/output.txt $D/solutions/day07.mjs
1213
echo ""

0 commit comments

Comments
 (0)