From a7da60bc86315f38e7ed3425570f919ae1dc499b Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Thu, 11 Dec 2025 07:06:19 -0600 Subject: [PATCH] feat: solve day 6 part 1 --- README.md | 2 +- cmd/06/main.go | 81 +++++++++++++++++++++++++++++++++++++++++++++ cmd/06/main_test.go | 27 +++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 cmd/06/main.go create mode 100644 cmd/06/main_test.go diff --git a/README.md b/README.md index 4490c73..95b41c3 100644 --- a/README.md +++ b/README.md @@ -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/) | | diff --git a/cmd/06/main.go b/cmd/06/main.go new file mode 100644 index 0000000..20389ea --- /dev/null +++ b/cmd/06/main.go @@ -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 +} diff --git a/cmd/06/main_test.go b/cmd/06/main_test.go new file mode 100644 index 0000000..1a93b56 --- /dev/null +++ b/cmd/06/main_test.go @@ -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) + } +}