2023-12-05 16:11:17 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
2023-12-06 11:33:24 +02:00
|
|
|
from dataclasses import dataclass, field
|
2023-12-05 16:11:17 +02:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class Range:
|
|
|
|
|
target: int
|
|
|
|
|
source: int
|
|
|
|
|
length: int
|
2023-12-06 11:33:24 +02:00
|
|
|
endpoint: int = field(init=False)
|
|
|
|
|
change: int = field(init=False)
|
|
|
|
|
|
|
|
|
|
def __post_init__(self):
|
|
|
|
|
self.endpoint = self.source + self.length
|
|
|
|
|
self.change = self.target - self.source
|
2023-12-05 16:11:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class SR:
|
2023-12-06 11:33:24 +02:00
|
|
|
startpoint: int
|
2023-12-05 16:11:17 +02:00
|
|
|
length: int
|
2023-12-06 11:33:24 +02:00
|
|
|
endpoint: int = field(init=False)
|
2023-12-05 16:11:17 +02:00
|
|
|
|
2023-12-06 11:33:24 +02:00
|
|
|
def __post_init__(self):
|
|
|
|
|
self.endpoint = self.startpoint + self.length
|
2023-12-05 16:11:17 +02:00
|
|
|
|
2023-12-05 19:22:00 +02:00
|
|
|
|
2023-12-06 11:33:24 +02:00
|
|
|
def getrangesource(range):
|
|
|
|
|
return range.source
|
2023-12-05 16:11:17 +02:00
|
|
|
|
|
|
|
|
|
2023-12-06 11:33:24 +02:00
|
|
|
def findmapping(sr, tables, prefix):
|
|
|
|
|
print(f"{prefix}Rekursiooni järel {len(tables)}")
|
|
|
|
|
print(f"{prefix}{sr}")
|
|
|
|
|
allresults = []
|
|
|
|
|
if len(tables) == 0:
|
|
|
|
|
print(f"{prefix}Tagasi, {sr.startpoint}")
|
|
|
|
|
return sr.startpoint
|
|
|
|
|
table = tables.pop(0)
|
|
|
|
|
table.sort(key=getrangesource)
|
|
|
|
|
print(f"{prefix}{table}")
|
|
|
|
|
otsas = False
|
|
|
|
|
for row in table:
|
|
|
|
|
if sr.endpoint < row.source:
|
|
|
|
|
# Väiksem kui käesolev mapping
|
|
|
|
|
print(f"{prefix}P1")
|
|
|
|
|
allresults.append(findmapping(sr, tables, prefix + "#"))
|
|
|
|
|
otsas = True
|
|
|
|
|
break
|
|
|
|
|
if sr.startpoint < row.source:
|
|
|
|
|
print(f"{prefix}P2")
|
|
|
|
|
# vahemik algus on enne käesoleva mappingu algust, lõpp kuskil hiljem
|
|
|
|
|
# kasutame ära vahemiku enne rea.source algust
|
|
|
|
|
allresults.append(
|
|
|
|
|
findmapping(
|
|
|
|
|
SR(sr.startpoint, row.source - sr.startpoint), tables, prefix + "#"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
if sr.endpoint > row.source:
|
|
|
|
|
print(f"{prefix}P2 Vana {sr}")
|
|
|
|
|
sr = SR(row.source, sr.endpoint - row.source)
|
|
|
|
|
print(f"{prefix}P2 Uus {sr}")
|
|
|
|
|
|
|
|
|
|
if sr.startpoint >= row.source:
|
|
|
|
|
if sr.endpoint < row.endpoint:
|
|
|
|
|
print(f"{prefix}P31")
|
|
|
|
|
print(f"{prefix}Mapping {row} {sr} {len(tables)}")
|
|
|
|
|
allresults.append(
|
|
|
|
|
findmapping(
|
|
|
|
|
SR(row.target + (sr.startpoint - row.source), sr.length),
|
|
|
|
|
tables,
|
|
|
|
|
prefix + "#",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
otsas = True
|
|
|
|
|
break
|
|
|
|
|
if sr.endpoint >= row.endpoint and sr.startpoint < row.endpoint:
|
|
|
|
|
print(f"{prefix}P32")
|
|
|
|
|
print(f"{prefix}Mapping {row} {sr} {len(tables)}")
|
|
|
|
|
allresults.append(
|
|
|
|
|
findmapping(
|
2023-12-06 16:12:31 +02:00
|
|
|
SR(row.target + (sr.startpoint - row.source), row.endpoint - sr.startpoint),
|
2023-12-06 11:33:24 +02:00
|
|
|
tables,
|
|
|
|
|
prefix + "#",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
if (sr.endpoint > row.endpoint):
|
|
|
|
|
print(f"{prefix}P32 Vana {sr}")
|
|
|
|
|
sr = SR(row.endpoint, sr.endpoint - row.endpoint)
|
|
|
|
|
print(f"{prefix}P32 Uus {sr}")
|
|
|
|
|
if not otsas:
|
|
|
|
|
allresults.append(findmapping(sr, tables, prefix + "#"))
|
|
|
|
|
|
|
|
|
|
print(f"{prefix}Vastused")
|
|
|
|
|
print(f"{prefix}{allresults}")
|
|
|
|
|
return min(allresults)
|
|
|
|
|
|
|
|
|
|
|
2023-12-06 16:12:31 +02:00
|
|
|
with open("data.txt") as file:
|
2023-12-05 16:11:17 +02:00
|
|
|
lines = [line.rstrip() for line in file]
|
|
|
|
|
|
|
|
|
|
tables = []
|
|
|
|
|
table = []
|
|
|
|
|
seeds = []
|
|
|
|
|
seedranges = []
|
|
|
|
|
for line in lines:
|
2023-12-06 11:33:24 +02:00
|
|
|
if line == "":
|
|
|
|
|
# print("Tühi rida")
|
2023-12-05 16:11:17 +02:00
|
|
|
continue
|
|
|
|
|
if line.startswith("seeds: "):
|
2023-12-06 11:33:24 +02:00
|
|
|
temp = re.findall(r"\d+", line)
|
2023-12-05 16:11:17 +02:00
|
|
|
seeds = list(map(int, temp))
|
2023-12-06 11:33:24 +02:00
|
|
|
# print(len(seeds))
|
|
|
|
|
for i in range(0, int(len(seeds) / 2)):
|
|
|
|
|
seedranges.append(SR(seeds[i * 2], seeds[i * 2 + 1]))
|
|
|
|
|
# print(seedranges)
|
2023-12-05 16:11:17 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if line[0].isalpha():
|
|
|
|
|
if len(table) > 0:
|
|
|
|
|
tables.append(table)
|
|
|
|
|
table = []
|
|
|
|
|
continue
|
|
|
|
|
|
2023-12-06 11:33:24 +02:00
|
|
|
temp = re.findall(r"\d+", line)
|
2023-12-05 16:11:17 +02:00
|
|
|
tableitems = list(map(int, temp))
|
|
|
|
|
table.append(Range(*tableitems))
|
|
|
|
|
tables.append(table)
|
|
|
|
|
|
|
|
|
|
|
2023-12-06 11:33:24 +02:00
|
|
|
# for table in tables:
|
|
|
|
|
# #print("tabel")
|
|
|
|
|
# for rida in table:
|
|
|
|
|
# print(rida)
|
2023-12-05 16:11:17 +02:00
|
|
|
|
|
|
|
|
pisim = 0
|
2023-12-06 11:33:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
print("########################")
|
|
|
|
|
print("####")
|
|
|
|
|
print(seedranges)
|
|
|
|
|
pisim = []
|
2023-12-05 16:11:17 +02:00
|
|
|
for sr in seedranges:
|
2023-12-06 11:33:24 +02:00
|
|
|
pisim.append(findmapping(sr, tables, ""))
|
2023-12-05 16:11:17 +02:00
|
|
|
|
|
|
|
|
|
2023-12-06 11:33:24 +02:00
|
|
|
print(min(pisim))
|