Day 11: Hex

This commit is contained in:
Meutel 2017-12-23 16:45:04 +01:00
parent ba23247555
commit 2e2bad9f57
1 changed files with 62 additions and 0 deletions

62
11dec/main.go Normal file
View File

@ -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
}