Finally solve 2019 day 12 and others
This commit is contained in:
36
2019/d12.py
36
2019/d12.py
@@ -1,4 +1,5 @@
|
||||
from lib import get_data, str_to_ints
|
||||
from math import lcm
|
||||
|
||||
|
||||
def freeze(coords, velos):
|
||||
@@ -40,7 +41,40 @@ def part_1(data):
|
||||
|
||||
|
||||
def part_2(data):
|
||||
raise Exception("TBD")
|
||||
steps = []
|
||||
for i in range(3):
|
||||
coords = []
|
||||
velos = []
|
||||
|
||||
for line in data.splitlines():
|
||||
coords.append(str_to_ints(line)[i])
|
||||
velos.append(0)
|
||||
|
||||
seen = {}
|
||||
for step in range(1_000_000):
|
||||
state = tuple(coords) + tuple(velos)
|
||||
if state in seen:
|
||||
steps.append(step)
|
||||
break
|
||||
else:
|
||||
seen[state] = step
|
||||
|
||||
for i in range(len(coords)):
|
||||
for j in range(i + 1, len(coords)):
|
||||
if coords[i] > coords[j]:
|
||||
velos[i] -= 1
|
||||
velos[j] += 1
|
||||
elif coords[i] < coords[j]:
|
||||
velos[i] += 1
|
||||
velos[j] -= 1
|
||||
|
||||
# update coords
|
||||
for i in range(len(coords)):
|
||||
coords[i] += velos[i]
|
||||
|
||||
# Intuition: Find when position repeats on each axis and then find lowest
|
||||
# common multiple for all three values.
|
||||
print(lcm(*steps))
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user