From 52422a9e16bb4dc101dd55fca1d99634e3d5146f Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Fri, 5 Dec 2025 07:05:28 -0600 Subject: [PATCH] feat: solve day 3 part 1 --- README.md | 2 +- cmd/03/bank/bank.go | 102 +++++++++++++++++++++++++++++++++++++++ cmd/03/bank/bank_test.go | 53 ++++++++++++++++++++ cmd/03/main.go | 18 +++++++ cmd/03/main_test.go | 27 +++++++++++ internal/file/file.go | 3 ++ 6 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 cmd/03/bank/bank.go create mode 100644 cmd/03/bank/bank_test.go create mode 100644 cmd/03/main.go create mode 100644 cmd/03/main_test.go diff --git a/README.md b/README.md index 07ba4d1..e33cdfc 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ go test ./cmd/01 |-----|-------------------------------------------------|-----------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 01 | [Problem](https://adventofcode.com/2025/day/1) | [Solution](./cmd/01/) | Part 1 seemed straight forward enough. Part 2 completely stumped me. The trickiest part was not counting zero's twice when you ended and then started from there. | | 02 | [Problem](https://adventofcode.com/2025/day/2) | [Solution](./cmd/02/) | Part 1 should have been super straight-forward, but some whitespace screwed up my parsing of the final range. I some how was able to stumble over a solution to part 2 rather blindly. | -| 03 | [Problem](https://adventofcode.com/2025/day/3) | [Solution](./cmd/03/) | | +| 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. | | 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | | | 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | | | 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | | diff --git a/cmd/03/bank/bank.go b/cmd/03/bank/bank.go new file mode 100644 index 0000000..40120f1 --- /dev/null +++ b/cmd/03/bank/bank.go @@ -0,0 +1,102 @@ +// Package bank represents a bank of batteries +package bank + +import ( + "fmt" + "strconv" +) + +type Bank interface { + Joltage() int + Cells() []int +} + +type bank struct { + cells []int +} + +func From(line string) Bank { + cells := []int{} + + for _, char := range line { + num, _ := strconv.Atoi(string(char)) + cells = append(cells, num) + } + + return bank{ + cells: cells, + } +} + +type cell struct { + index int + value int +} + +func (b bank) Joltage() int { + // TODO: We still need to + // find the max + // however we should then + // find the next max to the + // left of the highest + // and find the next max to + // the right of the higest + // we should then see what + // the large number we can make + // with these values + + max := cell{ + value: -1, + } + + for ci, cv := range b.cells { + if cv > max.value { + max = cell{ + index: ci, + value: cv, + } + } + } + + maxLeft := cell{ + value: -1, + } + + for ci, cv := range b.cells[:max.index] { + if cv > maxLeft.value { + maxLeft = cell{ + index: ci, + value: cv, + } + } + } + + maxRight := cell{ + value: -1, + } + + for ci, cv := range b.cells[max.index+1:] { + if cv > maxRight.value { + maxRight = cell{ + index: ci, + value: cv, + } + } + } + + maxInTensPlace := fmt.Sprintf("%d%d", max.value, maxRight.value) + maxInOnesPlace := fmt.Sprintf("%d%d", maxLeft.value, max.value) + + maxInTensPlaceAsNumber, _ := strconv.Atoi(maxInTensPlace) + maxInOnesPlaceAsNumber, _ := strconv.Atoi(maxInOnesPlace) + + if maxInTensPlaceAsNumber > maxInOnesPlaceAsNumber { + return maxInTensPlaceAsNumber + } + + return maxInOnesPlaceAsNumber +} + +func (b bank) Cells() []int { + return b.cells +} diff --git a/cmd/03/bank/bank_test.go b/cmd/03/bank/bank_test.go new file mode 100644 index 0000000..bb40284 --- /dev/null +++ b/cmd/03/bank/bank_test.go @@ -0,0 +1,53 @@ +package bank_test + +import ( + "slices" + "testing" + + "github.com/StevanFreeborn/advent-of-code-2025/cmd/03/bank" +) + +func TestFrom(t *testing.T) { + expected := []int{1, 2, 3, 4, 5} + input := "12345" + + result := bank.From(input) + + if slices.Equal(result.Cells(), expected) == false { + t.Errorf("got %v but wanted %v", result.Cells(), expected) + } +} + +type testCase struct { + bank bank.Bank + expected int +} + +func TestJoltage(t *testing.T) { + testCases := []testCase{ + { + bank: bank.From("987654321111111"), + expected: 98, + }, + { + bank: bank.From("811111111111119"), + expected: 89, + }, + { + bank: bank.From("234234234234278"), + expected: 78, + }, + { + bank: bank.From("818181911112111"), + expected: 92, + }, + } + + for _, testCase := range testCases { + result := testCase.bank.Joltage() + + if result != testCase.expected { + t.Errorf("got %d but wanted %d", result, testCase.expected) + } + } +} diff --git a/cmd/03/main.go b/cmd/03/main.go new file mode 100644 index 0000000..ea49008 --- /dev/null +++ b/cmd/03/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "github.com/StevanFreeborn/advent-of-code-2025/cmd/03/bank" + "github.com/StevanFreeborn/advent-of-code-2025/internal/file" +) + +func SolvePartOne(filePath string) int { + input := file.ReadLines(filePath) + total := 0 + + for _, line := range input { + bank := bank.From(line) + total += bank.Joltage() + } + + return total +} diff --git a/cmd/03/main_test.go b/cmd/03/main_test.go new file mode 100644 index 0000000..93a5cd9 --- /dev/null +++ b/cmd/03/main_test.go @@ -0,0 +1,27 @@ +package main_test + +import ( + "testing" + + solution "github.com/StevanFreeborn/advent-of-code-2025/cmd/03" +) + +func TestSolvePartOneWithExampleInput(t *testing.T) { + expected := 357 + + result := solution.SolvePartOne("EXAMPLE.txt") + + if result != expected { + t.Errorf("got %d but wanted %d", result, expected) + } +} + +func TestSolvePartOneWithInput(t *testing.T) { + expected := 17113 + + result := solution.SolvePartOne("INPUT.txt") + + if result != expected { + t.Errorf("got %d but wanted %d", result, expected) + } +} diff --git a/internal/file/file.go b/internal/file/file.go index 6154153..b0772a3 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -6,6 +6,9 @@ import ( "os" ) +// TODO: Implement a StreamLines method +// in the file package + func ReadLines(filePath string) []string { lines := []string{}