aoc 2017 (day 1-6)
This commit is contained in:
53
www/statics/aoc/2017/03_solution-2.py
Normal file
53
www/statics/aoc/2017/03_solution-2.py
Normal file
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import aoc
|
||||
import math
|
||||
import itertools
|
||||
|
||||
|
||||
# https://stackoverflow.com/questions/10094745
|
||||
def spiralpos(n):
|
||||
k = math.ceil((math.sqrt(n) - 1) / 2)
|
||||
t = 2 * k + 1
|
||||
m = t ** 2
|
||||
|
||||
t -= 1
|
||||
|
||||
if n >= m - t:
|
||||
return k - (m - n), -k
|
||||
|
||||
m -= t
|
||||
|
||||
if n >= m - t:
|
||||
return -k, -k + (m - n)
|
||||
|
||||
m -= t
|
||||
|
||||
if n >= m - t:
|
||||
return -k + (m - n), k
|
||||
|
||||
return k, k - (m - n - t)
|
||||
|
||||
|
||||
rawinput = aoc.read_input(3)
|
||||
intinput = int(rawinput)
|
||||
|
||||
dirs = [
|
||||
(-1, -1), (00, -1), (+1, -1),
|
||||
(-1, 00), (+1, 00),
|
||||
(-1, +1), (00, +1), (+1, +1),
|
||||
]
|
||||
|
||||
grid = dict()
|
||||
grid[(0, 0)] = 1
|
||||
|
||||
for i in itertools.count(2):
|
||||
x, y = spiralpos(i)
|
||||
v = 0
|
||||
for dx, dy in dirs:
|
||||
if (x + dx, y + dy) in grid:
|
||||
v += grid[(x + dx, y + dy)]
|
||||
if v > intinput:
|
||||
print(v)
|
||||
exit()
|
||||
grid[(x, y)] = v
|
||||
Reference in New Issue
Block a user