Compare commits

...

2 Commits

Author SHA1 Message Date
bb5764bd14 feat: solve part two 2025-12-22 10:55:56 +01:00
09176fad28 test: add unit tests for part two 2025-12-22 10:55:51 +01:00
2 changed files with 75 additions and 1 deletions

View File

@@ -46,5 +46,36 @@ func PartOne(data int) int {
} }
func PartTwo(data int) int { func PartTwo(data int) int {
return 0 grid := make(map[[2]int]int)
grid[[2]int{0, 0}] = 1
directions := [4][2]int{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
x, y := 1, 0
directionIdx := 0
sideLength := 1
stepsInSide := 1
for {
sum := grid[[2]int{x - 1, y - 1}] + grid[[2]int{x - 1, y}] + grid[[2]int{x - 1, y + 1}] +
grid[[2]int{x, y - 1}] + grid[[2]int{x, y + 1}] +
grid[[2]int{x + 1, y - 1}] + grid[[2]int{x + 1, y}] + grid[[2]int{x + 1, y + 1}]
if sum > data {
return sum
}
grid[[2]int{x, y}] = sum
if stepsInSide == sideLength {
stepsInSide = 0
if directionIdx%2 == 1 {
sideLength++
}
directionIdx = (directionIdx + 1) % 4
}
x += directions[directionIdx][0]
y += directions[directionIdx][1]
stepsInSide++
}
} }

View File

@@ -39,3 +39,46 @@ func TestPartOne(t *testing.T) {
}) })
} }
} }
func TestPartTwo(t *testing.T) {
tests := []struct {
name string
input int
expected int
}{
{
name: "input 1 returns first value larger than 1",
input: 1,
expected: 2,
},
{
name: "input 2 returns first value larger than 2",
input: 2,
expected: 4,
},
{
name: "input 4 returns first value larger than 4",
input: 4,
expected: 5,
},
{
name: "input 5 returns first value larger than 5",
input: 5,
expected: 10,
},
{
name: "input 747 returns first value larger than 747",
input: 747,
expected: 806,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := PartTwo(tt.input)
if got != tt.expected {
t.Errorf("PartTwo() = %d, want %d", got, tt.expected)
}
})
}
}