aoc 2017 (day 1-6)
This commit is contained in:
36
www/statics/aoc/2017/03_solution-1.py
Normal file
36
www/statics/aoc/2017/03_solution-1.py
Normal file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import aoc
|
||||
import math
|
||||
|
||||
|
||||
# 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)
|
||||
|
||||
x, y = spiralpos(intinput)
|
||||
|
||||
print(abs(x) + abs(y))
|
||||
Reference in New Issue
Block a user