diff --git a/README.md b/README.md index e33cdfc..13f4b6c 100644 --- a/README.md +++ b/README.md @@ -24,17 +24,17 @@ go test ./cmd/01 ## Challenges -| Day | Problem | Solution | Notes | -|-----|-------------------------------------------------|-----------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| 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/) | 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/) | | -| 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/) | | -| 10 | [Problem](https://adventofcode.com/2025/day/10) | [Solution](./cmd/10/) | | -| 11 | [Problem](https://adventofcode.com/2025/day/11) | [Solution](./cmd/11/) | | -| 12 | [Problem](https://adventofcode.com/2025/day/12) | [Solution](./cmd/12/) | | +| Day | Problem | Solution | Notes | +|-----|-------------------------------------------------|-----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| 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/) | 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/) | | +| 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | | +| 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | | +| 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/) | | +| 10 | [Problem](https://adventofcode.com/2025/day/10) | [Solution](./cmd/10/) | | +| 11 | [Problem](https://adventofcode.com/2025/day/11) | [Solution](./cmd/11/) | | +| 12 | [Problem](https://adventofcode.com/2025/day/12) | [Solution](./cmd/12/) | | diff --git a/cmd/03/bank/bank.go b/cmd/03/bank/bank.go index 40120f1..3ece212 100644 --- a/cmd/03/bank/bank.go +++ b/cmd/03/bank/bank.go @@ -2,12 +2,12 @@ package bank import ( - "fmt" "strconv" + "strings" ) type Bank interface { - Joltage() int + Joltage(numOfCellsToTurnOn int) int64 Cells() []int } @@ -28,73 +28,32 @@ func From(line string) Bank { } } -type cell struct { - index int - value int -} +func (b bank) Joltage(numOfCellsToTurnOn int) int64 { + bankLength := len(b.cells) + cellsOn := []int{} + removalBudget := bankLength - numOfCellsToTurnOn -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, - } + for _, cell := range b.cells { + for len(cellsOn) > 0 && removalBudget > 0 && cell > cellsOn[len(cellsOn)-1] { + cellToRemoveIndex := len(cellsOn) - 1 + cellsOn = cellsOn[:cellToRemoveIndex] + removalBudget-- } + + cellsOn = append(cellsOn, cell) } - maxLeft := cell{ - value: -1, + var sb strings.Builder + + for _, n := range cellsOn[:numOfCellsToTurnOn] { + sb.WriteString(strconv.Itoa(n)) } - for ci, cv := range b.cells[:max.index] { - if cv > maxLeft.value { - maxLeft = cell{ - index: ci, - value: cv, - } - } - } + finalString := sb.String() - maxRight := cell{ - value: -1, - } + finalJoltage, _ := strconv.ParseInt(finalString, 10, 64) - 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 + return finalJoltage } func (b bank) Cells() []int { diff --git a/cmd/03/bank/bank_test.go b/cmd/03/bank/bank_test.go index bb40284..a3e439e 100644 --- a/cmd/03/bank/bank_test.go +++ b/cmd/03/bank/bank_test.go @@ -19,34 +19,59 @@ func TestFrom(t *testing.T) { } type testCase struct { - bank bank.Bank - expected int + numberOfBanksToTurnOn int + bank bank.Bank + expected int } func TestJoltage(t *testing.T) { testCases := []testCase{ + // { + // numberOfBanksToTurnOn: 2, + // bank: bank.From("987654321111111"), + // expected: 98, + // }, { - bank: bank.From("987654321111111"), - expected: 98, - }, - { - bank: bank.From("811111111111119"), - expected: 89, - }, - { - bank: bank.From("234234234234278"), - expected: 78, - }, - { - bank: bank.From("818181911112111"), - expected: 92, + numberOfBanksToTurnOn: 2, + bank: bank.From("811111111111119"), + expected: 89, }, + // { + // numberOfBanksToTurnOn: 2, + // bank: bank.From("234234234234278"), + // expected: 78, + // }, + // { + // numberOfBanksToTurnOn: 2, + // bank: bank.From("818181911112111"), + // expected: 92, + // }, + // { + // numberOfBanksToTurnOn: 12, + // bank: bank.From("987654321111111"), + // expected: 987654321111, + // }, + // { + // numberOfBanksToTurnOn: 12, + // bank: bank.From("811111111111119"), + // expected: 811111111119, + // }, + // { + // numberOfBanksToTurnOn: 12, + // bank: bank.From("234234234234278"), + // expected: 434234234278, + // }, + // { + // numberOfBanksToTurnOn: 12, + // bank: bank.From("818181911112111"), + // expected: 888911112111, + // }, } for _, testCase := range testCases { - result := testCase.bank.Joltage() + result := testCase.bank.Joltage(testCase.numberOfBanksToTurnOn) - if result != testCase.expected { + if result != int64(testCase.expected) { t.Errorf("got %d but wanted %d", result, testCase.expected) } } diff --git a/cmd/03/main.go b/cmd/03/main.go index ea49008..ae793fe 100644 --- a/cmd/03/main.go +++ b/cmd/03/main.go @@ -5,13 +5,13 @@ import ( "github.com/StevanFreeborn/advent-of-code-2025/internal/file" ) -func SolvePartOne(filePath string) int { +func Solve(filePath string, numOfCells int) int64 { input := file.ReadLines(filePath) - total := 0 + total := int64(0) for _, line := range input { bank := bank.From(line) - total += bank.Joltage() + total += bank.Joltage(numOfCells) } return total diff --git a/cmd/03/main_test.go b/cmd/03/main_test.go index 93a5cd9..dc7b385 100644 --- a/cmd/03/main_test.go +++ b/cmd/03/main_test.go @@ -7,9 +7,9 @@ import ( ) func TestSolvePartOneWithExampleInput(t *testing.T) { - expected := 357 + expected := int64(357) - result := solution.SolvePartOne("EXAMPLE.txt") + result := solution.Solve("EXAMPLE.txt", 2) if result != expected { t.Errorf("got %d but wanted %d", result, expected) @@ -17,9 +17,29 @@ func TestSolvePartOneWithExampleInput(t *testing.T) { } func TestSolvePartOneWithInput(t *testing.T) { - expected := 17113 + expected := int64(17113) - result := solution.SolvePartOne("INPUT.txt") + result := solution.Solve("INPUT.txt", 2) + + if result != expected { + t.Errorf("got %d but wanted %d", result, expected) + } +} + +func TestSolvePartTwoWithExampleInput(t *testing.T) { + expected := int64(3_121_910_778_619) + + result := solution.Solve("EXAMPLE.txt", 12) + + if result != expected { + t.Errorf("got %d but wanted %d", result, expected) + } +} + +func TestSolvePartTwoWithInput(t *testing.T) { + expected := int64(169_709_990_062_889) + + result := solution.Solve("INPUT.txt", 12) if result != expected { t.Errorf("got %d but wanted %d", result, expected)