master
E. Almqvist 4 years ago
parent c03f579e78
commit 22ce0cbe60
  1. 147
      2020/20.py

@ -96,7 +96,7 @@ def borderCheck(border, other):
else: else:
return False, other return False, other
fitsDict = dd(set) fitsDict = dd(dict)
# fits struc # fits struc
# #
@ -112,8 +112,6 @@ def getFits(borders, fits=fitsDict, ignore=[], ignoreBorder=[]):
ignoreLen = len(ignoreBorder) ignoreLen = len(ignoreBorder)
borLen = len(borders) borLen = len(borders)
print(ignoreLen, borLen)
if(ignoreLen >= borLen): if(ignoreLen >= borLen):
#print("goodbye") #print("goodbye")
return fits # if there is nothing to do then return the result return fits # if there is nothing to do then return the result
@ -141,7 +139,7 @@ def getFits(borders, fits=fitsDict, ignore=[], ignoreBorder=[]):
if(check): # if the two borders match: if(check): # if the two borders match:
fits[pid] = fits[pid] or dict() fits[pid] = fits[pid] or dict()
fits[pid][pos] = (pid2, pos2) fits[pid][pos] = [pid2, pos2]
#ignore.append(pid) #ignore.append(pid)
ignoreBorder.append(bor2) ignoreBorder.append(bor2)
@ -150,11 +148,8 @@ def getFits(borders, fits=fitsDict, ignore=[], ignoreBorder=[]):
return getFits(newborders, fits, ignore) # do the other borders return getFits(newborders, fits, ignore) # do the other borders
print(picBorders)
fits = getFits(picBorders, fitsDict) fits = getFits(picBorders, fitsDict)
print("------------")
print(fits)
fitsListKeys = list(fits.keys()) fitsListKeys = list(fits.keys())
@ -184,82 +179,120 @@ def setPos(pid, x, y, pmap, coords, seen=[]):
pmap[y][x] = pid pmap[y][x] = pid
coords[pid] = (x, y) coords[pid] = (x, y)
seen.append(pid) seen.add(pid)
return seen return seen
def getPos(x, y, pmap):
try:
pid = pmap[y][x]
if( pid == "####" ):
return None
else:
return pid
except:
return None
def printMap(pmap): def printMap(pmap):
print(" #---# ") for y in range(mapWidth):
for y in range(mapWidth, -5, -1):
row = pmap[y] row = pmap[y]
for x in range(-8, mapWidth): for x in range(mapWidth):
try: try:
print( row[x], end=" " ) print( row[x], end=" " )
except: except:
print("----", end=" ") print("####", end=" ")
print("") print("\n")
print(" #---# ")
def findContainer(sid, fits):
foundContainers = []
for pid, fit in fits.items():
for f in fit.values():
if(sid in f):
foundContainers.append(pid)
def genTheMap(pmap, coords, seenPID=[], fits=fits): return foundContainers
i = 0
for pid, fit in fits.items():
print("#", pid, fit)
if( i == 0 ):
seenPID = setPos(pid, 0, 0, pmap, coords, seenPID)
print(f"{pid} at 0, 0")
for cpos, child in fit.items(): corners = []
cx, cy = translatePos(cpos, 0, 0) cornersPos = dict()
print(f"{child[0]} at {cx} {cy}") for pid, cont in fits.items():
seenPID = setPos(child[0], cx, cy, pmap, coords, seenPID) print(pid, cont)
if(len(cont) == 2):
corners.append(pid)
i += 1
else:
if( pid in seenPID ): # check if pid is in map
# if exists then check around it
print(f"{pid=} : {fit=}")
for cpos, child in fit.items(): prod = 1
if(not child[0] in seenPID): for c in corners:
prod *= int(c)
x, y = coords[pid] print("Part1:", prod)
cx, cy = translatePos(cpos, x, y)
print(f"{child=}: {child[0]} at {cx} {cy}") print("\n\n")
seenPID = setPos(child[0], cx, cy, pmap, coords, seenPID)
else: seenid = set()
print(f"{pid=} : {fit=}")
for seenp in seenPID: # check if anyone has the PID as a connection # know the corners
if(seenp == pid): # this should not happen # start from corner 1 then 2, then update 3 and 4
continue #
# 1---------2
# | |
# | stuff |
# | here |
# | |
# 3---------4
fit2 = fits[seenp] # append the first corner at 0, 0
for pos, fit in fit2.items(): mapW = mapWidth - 1
fid = fit[0]
if( fid == pid ): # found it
pos = flip(pos) # flip it because it is reversed
x, y = coords[seenp] # get the seen ones pos for y in range(mapWidth):
cx, cy = translatePos(pos, x, y) # get our pos for x in range(mapWidth):
picmap[y][x] = "####"
seenPID = setPos(pid, cx, cy, pmap, coords, seenPID) seenid = setPos(corners[0], 0, 0, picmap, piccoords, seenid) # some corner
print("####", corners)
i = 0
# NOTE: LOOP AGAIN IF LOST PIDS def getRotIndex(rot):
i += 1 return ["U", "R", "D", "L"].index(rot)
if(len(pmap) < len(pics)): def rotateDirs(pid, rot, fits):
return genTheMap(pmap, coords, seenPID, fits) irot = getRotIndex(rot)
else: cont = fits[pid]
return fits
for pos, stuff in cont.items():
ipos = getRotIndex(pos)
diff = ipos - irot
def genPicMap(pmap=picmap, pcoords=piccoords, fits=fits, seenid=set()):
for y in range(mapWidth):
for x in range(mapWidth):
pidAtPos = getPos(x, y, pmap)
if(pidAtPos):
fit = fits[pidAtPos] # possible connections
print(f"\n{pidAtPos}: {fit}")
for pos, child in fit.items():
if(child[0] in seenid):
continue
nx, ny = translatePos(pos, x, y)
pidInSpace = getPos(nx, ny, pmap) # check if there is something there
print(f"{pos=} {child=} : {pidInSpace=} {nx,ny=}")
if(pidInSpace or nx < 0 or ny < 0):
continue
seenid = setPos(child[0], nx, ny, pmap, pcoords, seenid)
genPicMap()
genTheMap(picmap, piccoords)
print("")
printMap(picmap) printMap(picmap)

Loading…
Cancel
Save