|
import json |
|
import os |
|
|
|
|
|
path = "./prime/1000.json" |
|
with open(path, 'r') as f: |
|
data = json.load(f) |
|
|
|
def combine_triplets_by_boundary_no_loops(NTar): |
|
combined = NTar[:] |
|
changes = True |
|
|
|
while changes: |
|
changes = False |
|
new_combined = [] |
|
used = [False] * len(combined) |
|
|
|
for i in range(len(combined)): |
|
if used[i]: |
|
continue |
|
|
|
current_triplet = combined[i] |
|
used[i] = True |
|
merged = False |
|
|
|
for j in range(len(combined)): |
|
if i != j and not used[j]: |
|
other_triplet = combined[j] |
|
|
|
|
|
if current_triplet[-1] == other_triplet[0]: |
|
current_triplet.extend(other_triplet[1:]) |
|
used[j] = True |
|
merged = True |
|
changes = True |
|
break |
|
elif current_triplet[0] == other_triplet[-1]: |
|
current_triplet = other_triplet[:-1] + current_triplet |
|
used[j] = True |
|
merged = True |
|
changes = True |
|
break |
|
|
|
|
|
new_combined.append(current_triplet) |
|
|
|
|
|
combined = new_combined |
|
|
|
return combined |
|
|
|
|
|
def combine_tar_ntar(tar, ntar): |
|
for nt in ntar: |
|
for i in range(len(tar)): |
|
if tar[i][-1] == nt[0]: |
|
tar[i] = tar[i]+nt[1:] |
|
elif tar[i][0] == nt[-1]: |
|
tar[i] = nt[:-1]+tar[i] |
|
return tar |
|
|
|
def check_order(routes, target): |
|
for i in range(len(routes)): |
|
if routes[i][-1] != target: |
|
if routes[i][0] != target: |
|
raise ValueError(f"Wrong order: {routes[i]}") |
|
else: |
|
routes[i] = routes[i][::-1] |
|
return routes |
|
|
|
routes_list = [] |
|
restrictions_list = [] |
|
for i in range(len(data)): |
|
triplets = data[i]['Triplets'] |
|
target = data[i]['Target'] |
|
restrictions = data[i]['Restriction'] |
|
tar = [] |
|
ntar = [] |
|
for tp in triplets: |
|
if target in tp: |
|
tar.append(tp) |
|
else: |
|
ntar.append(tp) |
|
if len(ntar) > 0: |
|
ntar = combine_triplets_by_boundary_no_loops(ntar) |
|
routes = combine_tar_ntar(tar, ntar) |
|
else: |
|
routes = tar |
|
print(target) |
|
routes = check_order(routes, target) |
|
routes_list.append(routes) |
|
restrictions_list.append(restrictions) |
|
|
|
|