feat: solve day 3 part 2
This commit is contained in:
+20
-61
@@ -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 {
|
||||
|
||||
+43
-18
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -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
|
||||
|
||||
+24
-4
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user