refactor: prepare part two, which will use the same parsing logic + new structure
This commit is contained in:
@@ -4,22 +4,26 @@ import (
|
|||||||
"advent-of-code/internal/registry"
|
"advent-of-code/internal/registry"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
rulePattern = regexp.MustCompile(`^(\w+ \w+) bags contain (.+)\.$`)
|
||||||
|
containedPattern = regexp.MustCompile(`(\d+) (\w+ \w+) bags?`)
|
||||||
|
)
|
||||||
|
|
||||||
|
type bagCount struct {
|
||||||
|
count int
|
||||||
|
bag string
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registry.Register("2020D7", ParseInput, PartOne, PartTwo)
|
registry.Register("2020D7", ParseInput, PartOne, PartTwo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseInput(filepath string) []string {
|
func buildForwardGraph(data []string) map[string][]bagCount {
|
||||||
content, _ := os.ReadFile(filepath)
|
forwardGraph := make(map[string][]bagCount)
|
||||||
return strings.Split(string(content), "\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
func PartOne(data []string) int {
|
|
||||||
reverseGraph := make(map[string][]string)
|
|
||||||
rulePattern := regexp.MustCompile(`^(\w+ \w+) bags contain (.+)\.$`)
|
|
||||||
containedPattern := regexp.MustCompile(`(\d+) (\w+ \w+) bags?`)
|
|
||||||
|
|
||||||
for _, line := range data {
|
for _, line := range data {
|
||||||
matches := rulePattern.FindStringSubmatch(line)
|
matches := rulePattern.FindStringSubmatch(line)
|
||||||
@@ -31,18 +35,38 @@ func PartOne(data []string) int {
|
|||||||
containedString := matches[2]
|
containedString := matches[2]
|
||||||
|
|
||||||
if containedString == "no other bags" {
|
if containedString == "no other bags" {
|
||||||
|
forwardGraph[containerBag] = []bagCount{}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
containedMatches := containedPattern.FindAllStringSubmatch(containedString, -1)
|
containedMatches := containedPattern.FindAllStringSubmatch(containedString, -1)
|
||||||
for _, match := range containedMatches {
|
for _, match := range containedMatches {
|
||||||
if len(match) >= 3 {
|
if len(match) >= 3 {
|
||||||
|
count, _ := strconv.Atoi(match[1])
|
||||||
containedBag := match[2]
|
containedBag := match[2]
|
||||||
reverseGraph[containedBag] = append(reverseGraph[containedBag], containerBag)
|
forwardGraph[containerBag] = append(forwardGraph[containerBag], bagCount{count: count, bag: containedBag})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return forwardGraph
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseInput(filepath string) []string {
|
||||||
|
content, _ := os.ReadFile(filepath)
|
||||||
|
return strings.Split(string(content), "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func PartOne(data []string) int {
|
||||||
|
forwardGraph := buildForwardGraph(data)
|
||||||
|
reverseGraph := make(map[string][]string)
|
||||||
|
|
||||||
|
for container, contained := range forwardGraph {
|
||||||
|
for _, child := range contained {
|
||||||
|
reverseGraph[child.bag] = append(reverseGraph[child.bag], container)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
visited := make(map[string]bool)
|
visited := make(map[string]bool)
|
||||||
queue := reverseGraph["shiny gold"]
|
queue := reverseGraph["shiny gold"]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user