diff --git a/11/11.go b/11/11.go index 8bcec4d..6e98a7f 100644 --- a/11/11.go +++ b/11/11.go @@ -13,46 +13,51 @@ import ( func main() { scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { s := strings.Split(scanner.Text(), ",") + fmt.Println(CalcDistance(s)) + } +} - x := 0 - ne := 0 - max := 0 - - // I had a crack at this with an approximation of a cube based coordinate system - // but the maths is hard and I suck. In the end I sought inspiration from - // https://www.redblobgames.com/grids/hexagons/ - and am now using axial coordinates - for dir := range s { - switch s[dir] { - case "n": - ne++ - case "s": - ne-- - case "se": - x++ - case "sw": - x-- - ne-- - case "ne": - x++ - ne++ - case "nw": - x-- - } - - // Inefficient, but I'm running out of lunchtime. - cd := getDistance(x, ne) - if cd > max { - max = cd - } +func CalcDistance(s []string) []int { + + x := 0 + ne := 0 + max := 0 + // I had a crack at this with an approximation of a cube based coordinate system + // but the maths is hard and I suck. In the end I sought inspiration from + // https://www.redblobgames.com/grids/hexagons/ - and am now using axial coordinates + for dir := range s { + switch s[dir] { + case "n": + ne++ + case "s": + ne-- + case "se": + x++ + case "sw": + x-- + ne-- + case "ne": + x++ + ne++ + case "nw": + x-- } - fmt.Println(getDistance(x, ne), max) + // Inefficient, but I'm running out of lunchtime. + cd := getDistance(x, ne) + if cd > max { + max = cd + } } + + cd := getDistance(x, ne) + + return []int{cd, max} + } func getDistance(x int, ne int) int { diff --git a/11/11_test.go b/11/11_test.go new file mode 100644 index 0000000..fc52dd3 --- /dev/null +++ b/11/11_test.go @@ -0,0 +1,30 @@ +package main + +import ( + "testing" +) + +type testpair struct { + values []string + result int + } + + var tests = []testpair{ + {[]string{"ne","ne","ne"}, 3}, + {[]string{"ne","ne","sw","sw"}, 0}, + {[]string{"ne","ne","s","s"}, 2}, + {[]string{"se","sw","se","sw","sw"}, 3}, + } + +func TestCalcDistance(t *testing.T) { + for _, pair := range tests { + v := CalcDistance(pair.values) + if v[0] != pair.result { + t.Error( + "For", pair.values, + "expected", pair.result, + "got", v[0], + ) + } + } +} \ No newline at end of file