Solution to problem 10 in Python

This commit is contained in:
David Doblas Jiménez 2022-05-29 12:41:40 +02:00
parent 12c4debb31
commit f4a1521559

View File

@ -164,6 +164,7 @@
import re
from operator import itemgetter
import matplotlib.pyplot as plt
import numpy as np
with open("files/P10.txt") as f:
@ -179,8 +180,8 @@ def is_complete_row(line):
def find_time(arr):
# get position and velocity arrays
pos = arr[:, :2]
vel = arr[:, 2:]
pos = arr[:, :2].copy()
vel = arr[:, 2:].copy()
found = False
cnt = 0
@ -193,3 +194,39 @@ def find_time(arr):
message_time = find_time(points_arr)
def find_message(arr, time):
pos = arr[:, :2].copy()
vel = arr[:, 2:].copy()
# get position when everything is lined up
pos += time * vel
# create canvas full of zeros big enough
# add one pixel to avoid IndexError when adding one during next step
canvas = np.zeros(pos.max(axis=0) + 1, dtype=np.int8) # (194, 132)
# mark aligned pixels in the canvas by adding 1
canvas[pos[:, 0], pos[:, 1]] += 1
# show only pixels in canvas from the minimum of pos array
# this will show only the word
minx, miny = pos.min(axis=0) # 132, 122
plt.imshow(canvas[minx:, miny:].T)
plt.show()
find_message(arr=points_arr, time=message_time)
# --- Part Two ---
# Good thing you didn't have to wait, because that would have taken a long
# time - much longer than the 3 seconds in the example above.
# Impressed by your sub-hour communication capabilities, the Elves are curious:
# exactly how many seconds would they have needed to wait for that message to
# appear?
print(f"Message appear at {message_time} s")