From e931b515a2d2257cda87f555d1bb697b39f92357 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Thu, 11 Dec 2025 10:33:58 -0600 Subject: [PATCH] refactor: make not ugly --- cmd/06/main.go | 74 +++++++--------------------------- cmd/06/problem/problem.go | 37 +++++++++++++++++ cmd/06/problem/problem_test.go | 19 +++++++++ 3 files changed, 70 insertions(+), 60 deletions(-) create mode 100644 cmd/06/problem/problem.go create mode 100644 cmd/06/problem/problem_test.go diff --git a/cmd/06/main.go b/cmd/06/main.go index 20389ea..cece549 100644 --- a/cmd/06/main.go +++ b/cmd/06/main.go @@ -1,81 +1,35 @@ package main import ( - "regexp" "strconv" + "strings" + "github.com/StevanFreeborn/advent-of-code-2025/cmd/06/problem" "github.com/StevanFreeborn/advent-of-code-2025/internal/file" ) func SolvePartOne(filePath string) int { input := file.ReadAllLines(filePath) - inputLength := len(input) - lastLineIndex := inputLength - 1 - - operandLines := input[:lastLineIndex] - operators := input[lastLineIndex] - listOfOperands := [][]int{} - - for _, line := range operandLines { - parsedOperands := parseOperands(line) - listOfOperands = append(listOfOperands, parsedOperands) - } - - ops := parseOperators(operators) + numOfRows := len(input) + numOfCols := len(strings.Fields(input[0])) + operatorRowNumber := numOfRows - 1 + operators := strings.Fields(input[operatorRowNumber]) total := 0 - for i := range len(ops) { - operator := ops[i] + for col := range numOfCols { + operator := operators[col] + operands := []int{} - result := 0 - - for _, operands := range listOfOperands { - if result == 0 { - result = operands[i] - continue - } - - if operator == "*" { - result *= operands[i] - continue - } - - if operator == "+" { - result += operands[i] - continue - } + for row := range operatorRowNumber { + operand, _ := strconv.Atoi(strings.Fields(input[row])[col]) + operands = append(operands, operand) } + problem := problem.From(operator, operands) + result := problem.Solve() total += result } return total } - -func parseOperators(line string) []string { - operatorsRegex := regexp.MustCompile(`(\*|\+)`) - matches := operatorsRegex.FindAllStringSubmatch(line, -1) - - operators := []string{} - - for _, match := range matches { - operators = append(operators, match[1]) - } - - return operators -} - -func parseOperands(line string) []int { - numbersRegex := regexp.MustCompile(`(\d+)`) - matches := numbersRegex.FindAllStringSubmatch(line, -1) - - operands := []int{} - - for _, match := range matches { - num, _ := strconv.Atoi(match[1]) - operands = append(operands, num) - } - - return operands -} diff --git a/cmd/06/problem/problem.go b/cmd/06/problem/problem.go new file mode 100644 index 0000000..a0b2292 --- /dev/null +++ b/cmd/06/problem/problem.go @@ -0,0 +1,37 @@ +// Package problem provides models and methods for representing a problem. +package problem + +type Problem interface { + Solve() int +} + +type problem struct { + operator string + operands []int +} + +func From(operator string, operands []int) Problem { + return problem{ + operator: operator, + operands: operands, + } +} + +func (p problem) Solve() int { + result := 0 + + switch p.operator { + case "+": + for _, operand := range p.operands { + result += operand + } + case "*": + result = 1 + + for _, operand := range p.operands { + result *= operand + } + } + + return result +} diff --git a/cmd/06/problem/problem_test.go b/cmd/06/problem/problem_test.go new file mode 100644 index 0000000..f25dc70 --- /dev/null +++ b/cmd/06/problem/problem_test.go @@ -0,0 +1,19 @@ +package problem_test + +import ( + "testing" + + "github.com/StevanFreeborn/advent-of-code-2025/cmd/06/problem" +) + +func TestSolve(t *testing.T) { + expected := 6 + operator := "+" + operands := []int{1, 2, 3} + + result := problem.From(operator, operands).Solve() + + if result != expected { + t.Errorf("expected %d, got %d", expected, result) + } +}