From 959c05b769143ceebb77f7770102d6f653327074 Mon Sep 17 00:00:00 2001 From: Kharec Date: Sun, 30 Nov 2025 12:55:52 +0100 Subject: [PATCH] feat: add part one solution --- internal/2018/DayTwo/code.go | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 internal/2018/DayTwo/code.go diff --git a/internal/2018/DayTwo/code.go b/internal/2018/DayTwo/code.go new file mode 100644 index 0000000..47cecab --- /dev/null +++ b/internal/2018/DayTwo/code.go @@ -0,0 +1,49 @@ +package daytwo + +import ( + "advent-of-code/internal/registry" + "os" + "strings" +) + +func init() { + registry.Register("2018D2", ParseInput, PartOne, PartTwo) +} + +func ParseInput(filepath string) []string { + content, _ := os.ReadFile(filepath) + return strings.Split(string(content), "\n") +} + +func PartOne(data []string) int { + countWithTwo := 0 + countWithThree := 0 + + for _, boxID := range data { + charCounts := make(map[rune]int) + for _, char := range boxID { + charCounts[char]++ + } + + hasExactlyTwo := false + hasExactlyThree := false + + for _, count := range charCounts { + switch count { + case 2: + hasExactlyTwo = true + case 3: + hasExactlyThree = true + } + } + + if hasExactlyTwo { + countWithTwo++ + } + if hasExactlyThree { + countWithThree++ + } + } + + return countWithTwo * countWithThree +}