refactor: make not ugly
This commit is contained in:
+14
-60
@@ -1,81 +1,35 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/StevanFreeborn/advent-of-code-2025/cmd/06/problem"
|
||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SolvePartOne(filePath string) int {
|
func SolvePartOne(filePath string) int {
|
||||||
input := file.ReadAllLines(filePath)
|
input := file.ReadAllLines(filePath)
|
||||||
inputLength := len(input)
|
numOfRows := len(input)
|
||||||
lastLineIndex := inputLength - 1
|
numOfCols := len(strings.Fields(input[0]))
|
||||||
|
operatorRowNumber := numOfRows - 1
|
||||||
operandLines := input[:lastLineIndex]
|
operators := strings.Fields(input[operatorRowNumber])
|
||||||
operators := input[lastLineIndex]
|
|
||||||
listOfOperands := [][]int{}
|
|
||||||
|
|
||||||
for _, line := range operandLines {
|
|
||||||
parsedOperands := parseOperands(line)
|
|
||||||
listOfOperands = append(listOfOperands, parsedOperands)
|
|
||||||
}
|
|
||||||
|
|
||||||
ops := parseOperators(operators)
|
|
||||||
|
|
||||||
total := 0
|
total := 0
|
||||||
|
|
||||||
for i := range len(ops) {
|
for col := range numOfCols {
|
||||||
operator := ops[i]
|
operator := operators[col]
|
||||||
|
operands := []int{}
|
||||||
|
|
||||||
result := 0
|
for row := range operatorRowNumber {
|
||||||
|
operand, _ := strconv.Atoi(strings.Fields(input[row])[col])
|
||||||
for _, operands := range listOfOperands {
|
operands = append(operands, operand)
|
||||||
if result == 0 {
|
|
||||||
result = operands[i]
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if operator == "*" {
|
|
||||||
result *= operands[i]
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if operator == "+" {
|
|
||||||
result += operands[i]
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
problem := problem.From(operator, operands)
|
||||||
|
result := problem.Solve()
|
||||||
total += result
|
total += result
|
||||||
}
|
}
|
||||||
|
|
||||||
return total
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user