Created
December 5, 2017 21:06
-
-
Save paulsmith/745124418eaf3160dbfc62d4c3fc6d37 to your computer and use it in GitHub Desktop.
AOC 2017 day 3 part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
) | |
type grid map[complex128]int | |
func (g grid) neighbors(pos complex128) int { | |
return g[pos+(-1+0i)] + | |
g[pos+(-1+1i)] + | |
g[pos+(0+1i)] + | |
g[pos+(1+1i)] + | |
g[pos+(1+0i)] + | |
g[pos+(1+-1i)] + | |
g[pos+(0+-1i)] + | |
g[pos+(-1+-1i)] | |
} | |
func (g grid) left(pos, dir complex128) bool { | |
return g[pos+(dir*(0+1i))] > 0 | |
} | |
func main() { | |
var input int | |
_, err := fmt.Sscanf(os.Args[1], "%d", &input) | |
if err != nil { | |
panic(err) | |
} | |
g := grid{ | |
0 + 0i: 1, | |
} | |
dir := 1 + 0i | |
pos := 1 + 0i | |
for ; g[pos-dir] < input; pos += dir { | |
g[pos] = g.neighbors(pos) | |
if !g.left(pos, dir) { | |
dir *= 0 + 1i // rotate left | |
} | |
} | |
fmt.Println(g[pos-dir]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment