Created
December 12, 2019 05:46
-
-
Save colinhowe/3cdb30332af590fae025f392dff58241 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inp = """<x=15, y=-2, z=-6> | |
<x=-5, y=-4, z=-11> | |
<x=0, y=-6, z=0> | |
<x=5, y=9, z=6>""" | |
inp = inp.split("\n") | |
coords = [ | |
line[1:-1].split(", ") | |
for line in inp | |
] | |
posns = [ | |
tuple([int(coord[2:]) for coord in line]) | |
for line in coords | |
] | |
vels = [ | |
(0, 0, 0) | |
] * len(posns) | |
# Find the interval along each axis as each one is indepenent | |
intervals = {} | |
for axis in range(3): | |
seen = set() | |
step = 0 | |
vals = [pos[axis] for pos in posns] | |
speeds = [0] * len(vals) | |
while True: | |
key = tuple(vals) + tuple(speeds) | |
if key in seen: | |
intervals[axis] = step | |
break | |
else: | |
seen.add(key) | |
new_speeds = [] | |
for i, (pos_a, vel_a) in enumerate(zip(vals, speeds)): | |
for j, pos_b, in enumerate(vals): | |
if pos_a < pos_b: | |
vel_a += 1 | |
elif pos_a > pos_b: | |
vel_a -= 1 | |
new_speeds.append(vel_a) | |
speeds = new_speeds | |
vals = [ | |
val + speed | |
for val, speed in zip(vals, speeds) | |
] | |
step += 1 | |
def gcd(a, b): | |
if a < b: | |
a, b = b, a | |
while b: | |
a, b = b, a % b | |
return a | |
print intervals | |
def compute_lcm(x, y): | |
lcm = (x*y)//gcd(x,y) | |
return lcm | |
print compute_lcm(intervals[0], compute_lcm(intervals[1], intervals[2])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment