mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
2017-day11: hex grid version of manhattan distance
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"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)
|
||||
|
||||
ans := hexEd(util.ReadFile("./input.txt"), part)
|
||||
fmt.Println("Output:", ans)
|
||||
}
|
||||
|
||||
var dirIndices = map[string]int{
|
||||
"n": 0,
|
||||
"ne": 1,
|
||||
"se": 2,
|
||||
"s": 3,
|
||||
"sw": 4,
|
||||
"nw": 5,
|
||||
}
|
||||
|
||||
func hexEd(input string, part int) int {
|
||||
steps := strings.Split(input, ",")
|
||||
tallyDirections := make([]int, 6)
|
||||
var furthest int // for part 2
|
||||
for _, step := range steps {
|
||||
tallyDirections[dirIndices[step]]++
|
||||
distanceFromStart := getDistanceFromOrigin(tallyDirections)
|
||||
furthest = mathutil.MaxInt(furthest, distanceFromStart)
|
||||
}
|
||||
|
||||
if part == 1 {
|
||||
return getDistanceFromOrigin(tallyDirections)
|
||||
}
|
||||
return furthest
|
||||
}
|
||||
|
||||
func getDistanceFromOrigin(tally []int) int {
|
||||
// zero out opposite indices, after this, there will be at most 3 positive
|
||||
// values in the slice
|
||||
for i := range tally {
|
||||
if tally[i] != 0 {
|
||||
oppositeIndex := (i + 3) % 6
|
||||
smaller := mathutil.MinInt(tally[oppositeIndex], tally[i])
|
||||
tally[oppositeIndex] -= smaller
|
||||
tally[i] -= smaller
|
||||
}
|
||||
}
|
||||
|
||||
// handle neighbors, which collapse into the current direction
|
||||
// e.g. sw,se == s
|
||||
for i := range tally {
|
||||
toLeft := (i + 5) % 6
|
||||
toRight := (i + 1) % 6
|
||||
if tally[toLeft] > 0 && tally[toRight] > 0 {
|
||||
smaller := mathutil.MinInt(tally[toLeft], tally[toRight])
|
||||
tally[toLeft] -= smaller
|
||||
tally[toRight] -= smaller
|
||||
tally[i] += smaller
|
||||
}
|
||||
}
|
||||
|
||||
distanceFromOrigin := mathutil.SumIntSlice(tally)
|
||||
|
||||
return distanceFromOrigin
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func Test_hexEd(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
part int
|
||||
want int
|
||||
}{
|
||||
{"actual-part1", util.ReadFile("input.txt"), 1, 794},
|
||||
{"actual-part2", util.ReadFile("input.txt"), 2, 1524},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := hexEd(tt.input, tt.part); got != tt.want {
|
||||
t.Errorf("hexEd() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user