kka pahasti
This commit is contained in:
135
5/advent_2.py
135
5/advent_2.py
@@ -1,8 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass, field
|
||||||
import re
|
import re
|
||||||
import sys
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -10,25 +9,96 @@ class Range:
|
|||||||
target: int
|
target: int
|
||||||
source: int
|
source: int
|
||||||
length: int
|
length: int
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class SR:
|
class SR:
|
||||||
source: int
|
startpoint: int
|
||||||
length: int
|
length: int
|
||||||
|
endpoint: int = field(init=False)
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
self.endpoint = self.startpoint + self.length
|
||||||
|
|
||||||
|
|
||||||
def findmapping(sr,tables):
|
def getrangesource(range):
|
||||||
|
return range.source
|
||||||
|
|
||||||
|
|
||||||
|
def findmapping(sr, tables, prefix):
|
||||||
|
print(f"{prefix}Rekursiooni järel {len(tables)}")
|
||||||
|
print(f"{prefix}{sr}")
|
||||||
|
allresults = []
|
||||||
if len(tables) == 0:
|
if len(tables) == 0:
|
||||||
return
|
print(f"{prefix}Tagasi, {sr.startpoint}")
|
||||||
|
return sr.startpoint
|
||||||
table = tables.pop(0)
|
table = tables.pop(0)
|
||||||
|
table.sort(key=getrangesource)
|
||||||
|
print(f"{prefix}{table}")
|
||||||
|
otsas = False
|
||||||
for row in table:
|
for row in table:
|
||||||
if sr.source >= row.source:
|
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}")
|
||||||
|
|
||||||
print(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(
|
||||||
|
SR(row.target + (sr.startpoint - row.source), sr.length),
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
with open("data.txt") as file:
|
with open("data2.txt") as file:
|
||||||
lines = [line.rstrip() for line in file]
|
lines = [line.rstrip() for line in file]
|
||||||
|
|
||||||
tables = []
|
tables = []
|
||||||
@@ -36,16 +106,16 @@ table = []
|
|||||||
seeds = []
|
seeds = []
|
||||||
seedranges = []
|
seedranges = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if line == '':
|
if line == "":
|
||||||
print('Tühi rida')
|
# print("Tühi rida")
|
||||||
continue
|
continue
|
||||||
if line.startswith("seeds: "):
|
if line.startswith("seeds: "):
|
||||||
temp = re.findall(r'\d+', line)
|
temp = re.findall(r"\d+", line)
|
||||||
seeds = list(map(int, temp))
|
seeds = list(map(int, temp))
|
||||||
print(len(seeds))
|
# print(len(seeds))
|
||||||
for i in range(0, int(len(seeds)/2)):
|
for i in range(0, int(len(seeds) / 2)):
|
||||||
seedranges.append(SR(seeds[i*2], seeds[i*2+1]))
|
seedranges.append(SR(seeds[i * 2], seeds[i * 2 + 1]))
|
||||||
print(seedranges)
|
# print(seedranges)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if line[0].isalpha():
|
if line[0].isalpha():
|
||||||
@@ -54,35 +124,26 @@ for line in lines:
|
|||||||
table = []
|
table = []
|
||||||
continue
|
continue
|
||||||
|
|
||||||
temp = re.findall(r'\d+', line)
|
temp = re.findall(r"\d+", line)
|
||||||
tableitems = list(map(int, temp))
|
tableitems = list(map(int, temp))
|
||||||
table.append(Range(*tableitems))
|
table.append(Range(*tableitems))
|
||||||
tables.append(table)
|
tables.append(table)
|
||||||
|
|
||||||
|
|
||||||
for table in tables:
|
# for table in tables:
|
||||||
print("tabel")
|
# #print("tabel")
|
||||||
for rida in table:
|
# for rida in table:
|
||||||
print(rida)
|
# print(rida)
|
||||||
|
|
||||||
pisim = 0
|
pisim = 0
|
||||||
|
|
||||||
|
|
||||||
|
print("########################")
|
||||||
|
print("####")
|
||||||
|
print(seedranges)
|
||||||
|
pisim = []
|
||||||
for sr in seedranges:
|
for sr in seedranges:
|
||||||
findmapping(sr,tables)
|
pisim.append(findmapping(sr, tables, ""))
|
||||||
|
|
||||||
for table in tables:
|
|
||||||
muudetud = False
|
|
||||||
print("tabel")
|
|
||||||
for mapping in table:
|
|
||||||
if arv >= mapping.source and arv < (mapping.source + mapping.length):
|
|
||||||
print(mapping)
|
|
||||||
arv = mapping.target + (arv - mapping.source)
|
|
||||||
print(f"Uus arv: {arv}")
|
|
||||||
break
|
|
||||||
|
|
||||||
print(f"Vastus: {arv}")
|
|
||||||
if pisim == 0:
|
|
||||||
pisim = arv
|
|
||||||
if pisim > arv:
|
|
||||||
pisim = arv
|
|
||||||
|
|
||||||
print(pisim)
|
print(min(pisim))
|
||||||
|
|||||||
Reference in New Issue
Block a user