From a1018af7990977d8ce587add6cff44fab1dc884c Mon Sep 17 00:00:00 2001 From: Araozu Date: Mon, 11 Mar 2024 18:40:19 -0500 Subject: [PATCH] improve performance of day 7 part 1 --- solutions/day07.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/solutions/day07.go b/solutions/day07.go index ea7fa57..ed2eb69 100644 --- a/solutions/day07.go +++ b/solutions/day07.go @@ -57,21 +57,20 @@ func parseBagStatement(bagStatement string) Bag { return Bag{color: bagColor, contents: bagContents, contentsCount: -1} } -func bagContainsShinyGold(bag *Bag, bagMap map[string]Bag) bool { +func bagContainsShinyGold(bagColor string, bagMap map[string]Bag) bool { + bag := bagMap[bagColor] if bag.containsShinyGold { return true } // recursively search - for _, bagName := range bag.contents { - if bagName.color == "shiny gold" { + for _, nextBag := range bag.contents { + if nextBag.color == "shiny gold" { bag.containsShinyGold = true return true } - nextBag := bagMap[bagName.color] - - if bagContainsShinyGold(&nextBag, bagMap) { + if bagContainsShinyGold(nextBag.color, bagMap) { bag.containsShinyGold = true return true } @@ -85,18 +84,20 @@ func Day07Part01(isTest bool) int { groups := strings.Split(input, "\n") bags := make(map[string]Bag) + bagColors := make([]string, len(groups)) // parse and collect the bags - for _, statement := range groups { + for i, statement := range groups { parsedBag := parseBagStatement(statement) bags[parsedBag.color] = parsedBag + bagColors[i] = parsedBag.color } shinyGoldContainers := 0 // process the bags - for _, bag := range bags { - if bagContainsShinyGold(&bag, bags) { + for _, bagColor := range bagColors { + if bagContainsShinyGold(bagColor, bags) { shinyGoldContainers += 1 } }