-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
90 lines (75 loc) · 2.34 KB
/
day3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def next_x():
x = 0
i = 0
while True:
x = pow(2 * i + 1, 2) + i + 1
i += 1
yield (i * 2 + 1, x)
def walk_around(size, start, n):
# print('wa {}, {}, {}'.format(size, start, n))
if n == 1:
return (0, 0)
elif n == 2:
return (1, 0)
move = (0, 1)
current = start
limit = int(size / 2)
(x, y) = (limit, 0)
while current != n:
(x, y) = (x + move[0], y + move[1])
current += 1
if move == (0, 1) and y == limit:
move = (-1, 0)
elif move == (-1, 0) and x == -limit:
move = (0, -1)
elif move == (0, -1) and y == -limit:
move = (1, 0)
elif move == (1, 0) and x == limit + 1: # now go up
move = (0, 1)
print("x, y = {}, {}, move = {}, {}, current = {} looking for {}".
format(x, y, move[0], move[1], current, n))
print('found {}, {}'.format(x, y))
return (x, y)
def day3_coord(n):
gen = next_x()
(size, start) = next(gen)
(psize, pstart) = (1, 1)
while n > start:
# print('size {} start {} n {}'.format(size, start, n))
(psize, pstart) = (size, start)
(size, start) = next(gen)
# `psize` is the length of a side, `pstart` is the `(y = 0, x = psize/2)`
print('final size {} start {} n {}'.format(size, start, n))
return walk_around(psize, pstart, n)
def day3_dist(n):
(x, y) = day3_coord(n)
return abs(x) + abs(y)
def day3_find_biggest(n):
if n == 1:
return 1
if n == 2:
return 1
current = 2
board = {(0, 0): 1, (1, 0): 1}
subtot = 1
while subtot <= n:
current += 1
(x, y) = day3_coord(current)
subtot = \
board.get((x - 1, y - 1), 0) + \
board.get((x, y - 1), 0) + \
board.get((x + 1, y - 1), 0) + \
board.get((x + 1, y), 0) + \
board.get((x + 1, y + 1), 0) + \
board.get((x, y + 1), 0) + \
board.get((x - 1, y + 1), 0) + \
board.get((x - 1, y), 0)
board[(x, y)] = subtot
print(board)
print('im at {}, {}, value {} sum {}'.format(x, y, current, subtot))
return subtot
def main():
print('distance is {}'.format(day3_dist(361527)))
print('biggest is {}'.format(day3_find_biggest(361527)))
if __name__ == '__main__':
main()