+/AoC

advent of code

git clone https://git.julia.blue/+/AoC

Joseph RyanStart 2018!38c0746

master
920 B32 linesraw
1from math import sqrt
2
3n = 347991
4
5ring = 0
6while 4 * (ring**2 + ring) < n:
7    ring += 1
8ring -= 1
9temp = n - 4 * (ring**2 + ring) - 1
10
11print "Part 1:", ring * 2 + sum(([1] + [-1] * ring + ([1] * (ring + 1) + [-1] * (ring + 1)) * 3 + [1] * (ring + 1))[:temp])
12
13import numpy as np
14
15rotate = lambda x: np.asarray(np.matrix([x[0], x[1]]) * np.matrix([[0, 1], [-1, 0]]))[0]
16
17board = np.zeros((300, 300), np.int32)
18pos = np.array([150, 150])
19direction = np.array([1, 0])
20neighbors = [(i, j) for i in range(-1, 2) for j in range(-1, 2)]
21board[pos[0], pos[1]] = 1
22pos += direction
23
24for i in range(n):
25    board[pos[0], pos[1]] = sum(board[pos[0] + i[0], pos[1] + i[1]] for i in neighbors)
26    if board[pos[0], pos[1]] > n:
27        print "Part 2:", board[pos[0], pos[1]]
28        break
29    temp = rotate(direction)
30    if not board[pos[0] + temp[0], pos[1] + temp[1]]:
31        direction = rotate(direction)
32    pos += direction