feat: solve day 6 part 1
This commit is contained in:
@@ -31,7 +31,7 @@ go test ./cmd/01
|
||||
| 03 | [Problem](https://adventofcode.com/2025/day/3) | [Solution](./cmd/03/) | Part 1 intuitively makes you think you only care about highest nums, but then you need to consider placement. I once again tripped over the solution using a stack. However chat morally supported me the whole time. |
|
||||
| 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | Part 1 was by far the most straight-forward challenge so far. This was easiest day of AoC this year by far. |
|
||||
| 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | Part 1 was fairly straight forward. Merging ranges was trickiest bit. I feel like I'm being led into a trap with how straight forward today was. |
|
||||
| 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | |
|
||||
| 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | Part 1 I don't think it is the best solution, but it does solve it. |
|
||||
| 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | |
|
||||
| 08 | [Problem](https://adventofcode.com/2025/day/8) | [Solution](./cmd/08/) | |
|
||||
| 09 | [Problem](https://adventofcode.com/2025/day/9) | [Solution](./cmd/09/) | |
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"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)
|
||||
|
||||
total := 0
|
||||
|
||||
for i := range len(ops) {
|
||||
operator := ops[i]
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package main_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
solution "github.com/StevanFreeborn/advent-of-code-2025/cmd/06"
|
||||
)
|
||||
|
||||
func TestSolvePartOneWithExampleInput(t *testing.T) {
|
||||
expected := 4277556
|
||||
|
||||
result := solution.SolvePartOne("EXAMPLE.txt")
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("got %d but wanted %d", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSolvePartOneWithInput(t *testing.T) {
|
||||
expected := 6169101504608
|
||||
|
||||
result := solution.SolvePartOne("INPUT.txt")
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("got %d but wanted %d", result, expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user