From 2e2bad9f57b3bfcd16d3a67ad0801c18229dc83d Mon Sep 17 00:00:00 2001 From: Meutel Date: Sat, 23 Dec 2017 16:45:04 +0100 Subject: [PATCH] Day 11: Hex --- 11dec/main.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 11dec/main.go diff --git a/11dec/main.go b/11dec/main.go new file mode 100644 index 0000000..5cd1c2d --- /dev/null +++ b/11dec/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +func main() { + var moves []string = parseInput() + fmt.Println(moves) + x, y, z := 0, 0, 0 + for _, m := range moves { + x, y, z = move(x, y, z, m) + fmt.Println(x, y, z) + } + fmt.Println("Result:", max(max(abs(x), abs(y)), abs(z))) +} + +func parseInput() []string { + sc := bufio.NewScanner(os.Stdin) + sc.Scan() + return strings.Split(sc.Text(), ",") +} + +func move(x, y, z int, m string) (int, int, int) { + switch m { + case "n": + y++ + z-- + case "ne": + x++ + z-- + case "se": + x++ + y-- + case "s": + y-- + z++ + case "sw": + x-- + z++ + case "nw": + x-- + y++ + } + return x, y, z +} + +func abs(n int) int { + if n > 0 { + return n + } + return -n +} +func max(a, b int) int { + if a > b { + return a + } + return b +}