refactor: make not ugly

This commit is contained in:
Stevan Freeborn
2025-12-11 10:33:58 -06:00
parent 9b53e28a19
commit e931b515a2
3 changed files with 70 additions and 60 deletions
+14 -60
View File
@@ -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
}
+37
View File
@@ -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
}
+19
View File
@@ -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)
}
}