コード例 #1
0
f.close()

def parse_moon(string):
    m = re.search(r'<x=(-?\d+), y=(-?\d+), z=(-?\d+)>', string)
    x, y, z = int(m.group(1)), int(m.group(2)), int(m.group(3))
    return Moon(x, y, z)


# --- Part 1 ---

moons = [parse_moon(str) for str in inputs]
universe = Universe(moons)

for _ in range(1000):
    universe.step()
print(universe.total_energy())


# --- Part 2 ---

def lowest_common_multiple(nums):
    lcm = nums[0]
    for i in nums[1:]:
        lcm = int(lcm*i/math.gcd(lcm, i))
    return lcm

def steps_to_repeat_per_axis(universe):
    initial_universe = copy(universe)
    steps_to_repeat = []
    for axis in ['x', 'y', 'z']:
        moons = [parse_moon(str) for str in inputs]