2025-12-11 07:06:19 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strconv"
|
2025-12-11 10:33:58 -06:00
|
|
|
"strings"
|
2025-12-11 07:06:19 -06:00
|
|
|
|
2025-12-11 10:33:58 -06:00
|
|
|
"github.com/StevanFreeborn/advent-of-code-2025/cmd/06/problem"
|
2025-12-11 07:06:19 -06:00
|
|
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func SolvePartOne(filePath string) int {
|
|
|
|
|
input := file.ReadAllLines(filePath)
|
2025-12-11 10:33:58 -06:00
|
|
|
numOfRows := len(input)
|
|
|
|
|
numOfCols := len(strings.Fields(input[0]))
|
|
|
|
|
operatorRowNumber := numOfRows - 1
|
|
|
|
|
operators := strings.Fields(input[operatorRowNumber])
|
2025-12-11 07:06:19 -06:00
|
|
|
|
|
|
|
|
total := 0
|
|
|
|
|
|
2025-12-11 10:33:58 -06:00
|
|
|
for col := range numOfCols {
|
|
|
|
|
operator := operators[col]
|
|
|
|
|
operands := []int{}
|
2025-12-11 07:06:19 -06:00
|
|
|
|
2025-12-11 10:33:58 -06:00
|
|
|
for row := range operatorRowNumber {
|
|
|
|
|
operand, _ := strconv.Atoi(strings.Fields(input[row])[col])
|
|
|
|
|
operands = append(operands, operand)
|
2025-12-11 07:06:19 -06:00
|
|
|
}
|
|
|
|
|
|
2025-12-11 10:33:58 -06:00
|
|
|
problem := problem.From(operator, operands)
|
|
|
|
|
result := problem.Solve()
|
2025-12-11 07:06:19 -06:00
|
|
|
total += result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return total
|
|
|
|
|
}
|
2025-12-12 06:52:39 -06:00
|
|
|
|
|
|
|
|
func SolvePartTwo(filePath string) int {
|
|
|
|
|
input := file.ReadAllLines(filePath)
|
|
|
|
|
numOfRows := len(input)
|
|
|
|
|
operatorRowIndex := numOfRows - 1
|
|
|
|
|
operatorRow := input[operatorRowIndex]
|
|
|
|
|
operatorRowLength := len(operatorRow)
|
|
|
|
|
|
|
|
|
|
total := 0
|
|
|
|
|
operands := []int{}
|
|
|
|
|
|
|
|
|
|
for operatorIndex := operatorRowLength - 1; operatorIndex >= 0; operatorIndex-- {
|
|
|
|
|
var operandBuilder strings.Builder
|
|
|
|
|
|
|
|
|
|
for row := range numOfRows - 1 {
|
|
|
|
|
v := string(input[row][operatorIndex])
|
|
|
|
|
operandBuilder.WriteString(v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operandString := strings.TrimSpace(operandBuilder.String())
|
|
|
|
|
operand, _ := strconv.Atoi(operandString)
|
|
|
|
|
operands = append(operands, operand)
|
|
|
|
|
operandBuilder.Reset()
|
|
|
|
|
|
|
|
|
|
operator := string(operatorRow[operatorIndex])
|
|
|
|
|
|
|
|
|
|
if operator != " " {
|
|
|
|
|
total += problem.From(operator, operands).Solve()
|
|
|
|
|
operatorIndex--
|
|
|
|
|
operands = []int{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return total
|
|
|
|
|
}
|