Compare commits
2 Commits
cc29b51c3a
...
bb5764bd14
| Author | SHA1 | Date | |
|---|---|---|---|
| bb5764bd14 | |||
| 09176fad28 |
@@ -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++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user