Spaces:
Running
Running
File size: 3,543 Bytes
a4da721 |
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
def load_data(file):
with open(file) as f:
data = f.readlines()
positions = []
velocities = []
for line in data:
_, p, v = line.split("=")
p = [int(x.split()[0]) for x in p.split(",")]
v = [int(x) for x in v.split(",")]
positions.append(p)
velocities.append(v)
return positions, velocities
def move(n_seconds, pos, vel, W, H):
x, y = pos
vx, vy = vel
new_x = (x + vx * n_seconds) % W
new_y = (y + vy * n_seconds) % H
return (new_x, new_y)
def construct_grid(positions, W, H):
grid = [[0]*W for _ in range(H)]
for pos in positions:
x, y = pos
grid[y][x] += 1
return grid
def pprint(grid):
import copy
grid_copy = copy.deepcopy(grid)
for y in range(H):
for x in range(W):
grid_copy[y][x] = str(grid_copy[y][x]) if grid_copy[y][x] != 0 else "."
grid_str = "\n".join(["".join(g) for g in grid_copy])
print(grid_str)
def get_safety_factor(grid, H, W):
middle_x = W // 2
middle_y = H // 2
quadrants = [[0,0], [0,0]]
for y in range(H):
for x in range(W):
if x < middle_x and y < middle_y:
quadrants[0][0] += grid[y][x]
if x > middle_x and y < middle_y:
quadrants[1][0] += grid[y][x]
if x < middle_x and y > middle_y:
quadrants[0][1] += grid[y][x]
if x > middle_x and y > middle_y:
quadrants[1][1] += grid[y][x]
return quadrants[0][0] * quadrants[0][1] * quadrants[1][1] * quadrants[1][0]
pass
# Part one
file = "input.txt"
W, H = 101, 103
positions, velocities = load_data(file)
n_seconds = 100
new_positions = []
for pos, vel in zip(positions, velocities):
new_pos = move(n_seconds, pos, vel, W=W, H=H)
new_positions.append(new_pos)
grid = construct_grid(new_positions, W=W, H=H)
print(get_safety_factor(grid, H=H, W=W))
# Part two
import time
file = "input.txt"
W, H = 101, 103
positions, velocities = load_data(file)
grid = construct_grid(positions, W=W, H=H)
row_0 = grid[0]
row_1 = grid[1]
row_2 = grid[2]
new_positions = []
n_moves = 750+980+320+257+300+230+200+500+140+312+3297
for pos, vel in zip(positions, velocities):
new_pos = move(n_moves, pos, vel, W=W, H=H)
new_positions.append(new_pos)
positions = new_positions
grid = construct_grid(positions, W=W, H=H)
# pprint(grid)
print(n_moves)
# A bunch of code that was used for debugging!
# grid = construct_grid(positions, W=W, H=H)
# new_positions = []
# for pos, vel in zip(positions, velocities):
# new_pos = move(260+139+403+209+473, pos, vel, W=W, H=H)
# new_positions.append(new_pos)
# positions = new_positions
# row_tip = [0]*W
# # row_tip[50] = 1
#
# n_seconds = 1
# for N in range(n_seconds):
# new_positions = []
# for pos, vel in zip(positions, velocities):
# new_pos = move(1, pos, vel, W=W, H=H)
# new_positions.append(new_pos)
#
# positions = new_positions
#
# grid = construct_grid(positions, W=W, H=H)
#
# if row_0 == grid[0] and row_1 == grid[1] and row_2 == grid[2]:
# print("Repeat!!!!???!?", N)
# break
#
# # if row_tip == grid[0]:
# # print()
# # print(N)
# # pprint(grid)
# # print()
#
# # if N % 100 == 0:
# # print(N)
# pprint(grid)
# print(N)
# print()
# print()
# time.sleep(0.001)
# print(get_safety_factor(grid, H=H, W=W))
|