From 8915de6145d297925e27aeed15725196c2bf45d5 Mon Sep 17 00:00:00 2001 From: Kharec Date: Wed, 26 Nov 2025 16:05:47 +0100 Subject: [PATCH] feat: add 2022D1 solutions --- internal/2022/DayOne/code.go | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 internal/2022/DayOne/code.go diff --git a/internal/2022/DayOne/code.go b/internal/2022/DayOne/code.go new file mode 100644 index 0000000..d12a534 --- /dev/null +++ b/internal/2022/DayOne/code.go @@ -0,0 +1,62 @@ +package dayone + +import ( + "advent-of-code/internal/registry" + "cmp" + "os" + "slices" + "strconv" + "strings" +) + +func init() { + registry.Register("2022D1", ParseInput, PartOne, PartTwo) +} + +func ParseInput(filepath string) []string { + content, _ := os.ReadFile(filepath) + return strings.Split(string(content), "\n") +} + +func PartOne(input []string) int { + maxCalories, currentElf := 0, 0 + + for _, line := range input { + if line == "" { + maxCalories = max(maxCalories, currentElf) + currentElf = 0 + continue + } + val, _ := strconv.Atoi(line) + currentElf += val + } + + return max(maxCalories, currentElf) +} + +func PartTwo(input []string) int { + var totals []int + currentElf := 0 + + for _, line := range input { + if line == "" { + totals = append(totals, currentElf) + currentElf = 0 + continue + } + + val, _ := strconv.Atoi(line) + currentElf += val + } + totals = append(totals, currentElf) + + slices.SortFunc(totals, func(a, b int) int { + return cmp.Compare(b, a) + }) + + sum := 0 + for idx := 0; idx < 3 && idx < len(totals); idx++ { + sum += totals[idx] + } + return sum +}