2017-day3:spiraling around a point

This commit is contained in:
alexchao26
2020-12-13 20:28:50 -05:00
parent 7ccfca0897
commit eab6a4f4e3
2 changed files with 164 additions and 0 deletions
+123
View File
@@ -0,0 +1,123 @@
package main
import (
"flag"
"fmt"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
var part int
flag.IntVar(&part, "part", 1, "part 1 or 2")
flag.Parse()
fmt.Println("Running part", part)
if part == 1 {
ans := part1(util.ReadFile("./input.txt"))
fmt.Println("Output:", ans)
} else {
ans := part2(util.ReadFile("./input.txt"))
fmt.Println("Output:", ans)
}
}
func part1(input string) int {
inputNum := mathutil.StrToInt(input)
directions := [][2]int{
{0, 1}, // right
{-1, 0}, // north
{0, -1}, // west
{1, 0}, // south
}
coordsToSquareNum := map[[2]int]int{
[2]int{0, 0}: 1,
[2]int{0, 1}: 2,
}
// @ every step, check left, i.e. see if we can turn right
// if it's not in map, turn & move there
// otherwise continue adding in same direction
directionIndex := 0 // start facing right
row, col := 0, 1
number := 2
for len(coordsToSquareNum) < inputNum {
leftDiff := directions[(directionIndex+1)%4]
leftCoord := [2]int{row + leftDiff[0], col + leftDiff[1]}
if _, ok := coordsToSquareNum[leftCoord]; !ok {
// turn left first
directionIndex = (directionIndex + 1) % 4
}
// move to coordinate in front & add it to the map
diff := directions[directionIndex]
row += diff[0]
col += diff[1]
next := [2]int{row, col}
coordsToSquareNum[next] = number
number++ // increment number
}
return mathutil.ManhattanDistance(0, 0, row, col)
}
func part2(input string) int {
inputNum := mathutil.StrToInt(input)
directions := [][2]int{
{0, 1}, // right
{-1, 0}, // north
{0, -1}, // west
{1, 0}, // south
}
allNeighborDiffs := [][2]int{
{-1, -1},
{-1, 0},
{-1, 1},
{0, -1},
{0, 1},
{1, -1},
{1, 0},
{1, 1},
}
coordsToNeighborSum := map[[2]int]int{
[2]int{0, 0}: 1,
[2]int{0, 1}: 1,
}
// @ every step, check left, i.e. see if we can turn right
// if it's not in map, turn & move there
// otherwise continue adding in same direction
directionIndex := 0 // start facing right
row, col := 0, 1
for len(coordsToNeighborSum) < inputNum {
leftDiff := directions[(directionIndex+1)%4]
leftCoord := [2]int{row + leftDiff[0], col + leftDiff[1]}
if _, ok := coordsToNeighborSum[leftCoord]; !ok {
// turn left first
directionIndex = (directionIndex + 1) % 4
}
// move to coordinate in front & add it to the map
diff := directions[directionIndex]
row += diff[0]
col += diff[1]
next := [2]int{row, col}
var sum int
for _, d := range allNeighborDiffs {
sum += coordsToNeighborSum[[2]int{row + d[0], col + d[1]}]
}
if sum > inputNum {
return sum
}
coordsToNeighborSum[next] = sum
}
panic("uh something broke, return should occur in for loop")
}
+41
View File
@@ -0,0 +1,41 @@
package main
import (
"testing"
"github.com/alexchao26/advent-of-code-go/util"
)
func Test_part1(t *testing.T) {
tests := []struct {
name string
input string
want int
}{
{"actual", util.ReadFile("input.txt"), 326},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := part1(tt.input); got != tt.want {
t.Errorf("part1() = %v, want %v", got, tt.want)
}
})
}
}
func Test_part2(t *testing.T) {
tests := []struct {
name string
input string
want int
}{
{"actual", util.ReadFile("input.txt"), 363010},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := part2(tt.input); got != tt.want {
t.Errorf("part2() = %v, want %v", got, tt.want)
}
})
}
}