feat: solve day 3 part 1
This commit is contained in:
@@ -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. |
|
| 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. |
|
| 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/) | |
|
| 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | |
|
||||||
| 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | |
|
| 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | |
|
||||||
| 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | |
|
| 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | |
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TODO: Implement a StreamLines method
|
||||||
|
// in the file package
|
||||||
|
|
||||||
func ReadLines(filePath string) []string {
|
func ReadLines(filePath string) []string {
|
||||||
lines := []string{}
|
lines := []string{}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user