From 30936b6204bc142373489220d445273206fdc586 Mon Sep 17 00:00:00 2001 From: RPJosh Date: Sat, 2 Dec 2023 14:41:47 +0100 Subject: [PATCH] 2023-02 --- internal/2023/day_02/in.go | 94 +++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/internal/2023/day_02/in.go b/internal/2023/day_02/in.go index bc6c878..c4da8b1 100644 --- a/internal/2023/day_02/in.go +++ b/internal/2023/day_02/in.go @@ -1,11 +1,101 @@ package day_02 +import ( + "fmt" + "regexp" + "strings" + + "git.rpjosh.de/RPJosh/go-logger" + "rpjosh.de/adventOfCode/pkg/utils" +) + type Day struct{} func (d *Day) Part1(in string) string { - return "" + + // This map contains the maximum balls that are in the bag + balls := map[string]int{ + "red": 12, + "green": 13, + "blue": 14, + } + + // Regex to get the game number + gameNumberRegex := regexp.MustCompile(`^Game (?P\w*)`) + ballsRegex := regexp.MustCompile(`(?P\w* (blue|red|green))(,|;)? ?`) + + rtc := 0 + for _, val := range strings.Split(in, "\n") { + if val == "" { + continue + } + + // Get the game ID + game := utils.ToInt(gameNumberRegex.FindStringSubmatch(val)[1]) + possible := true + + for _, ballAll := range ballsRegex.FindAllStringSubmatch(val, -1) { + ballValues := strings.Split(ballAll[1], " ") + + // Extract count and color + count := utils.ToInt(ballValues[0]) + color := ballValues[1] + + logger.Debug("%d. Found balls %q", game, ballAll[1]) + + if count > balls[color] { + possible = false + break + } + } + + // Add the game to the counter + if possible { + rtc += game + } + } + + return fmt.Sprintf("%d", rtc) } func (d *Day) Part2(in string) string { - return "" + // Regex to get the game number + gameNumberRegex := regexp.MustCompile(`^Game (?P\w*)`) + ballsRegex := regexp.MustCompile(`(?P\w* (blue|red|green))(,|;)? ?`) + + rtc := 0 + for _, val := range strings.Split(in, "\n") { + if val == "" { + continue + } + + // Get the game ID + game := utils.ToInt(gameNumberRegex.FindStringSubmatch(val)[1]) + + // Maximum balls count present in the game + balls := map[string]int{ + "red": 0, + "green": 0, + "blue": 0, + } + + for _, ballAll := range ballsRegex.FindAllStringSubmatch(val, -1) { + ballValues := strings.Split(ballAll[1], " ") + + // Extract count and color + count := utils.ToInt(ballValues[0]) + color := ballValues[1] + + logger.Debug("%d. Found balls %q", game, ballAll[1]) + + if count > balls[color] { + balls[color] = count + } + } + + // Add the power to the result + rtc += balls["red"] * balls["green"] * balls["blue"] + } + + return fmt.Sprintf("%d", rtc) }