Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.4 kB
2
Indexable
Never
input = open("18.txt").read().strip().split("\n")
from copy import deepcopy 

path = [((0,0),(0,0))]
#((y,new y),(x,new x))
for dig in input:
    dig = dig.split("#")
    dir = int(dig[1][-2])
    length = ""
    for l in range(5):
        length += dig[1][l]
    length = int(length,base = 16)
    if dir == 3:
        path.append(((path[-1][0][1],path[-1][0][1]-length),(path[-1][1][1],path[-1][1][1])))
    elif dir == 1:
        path.append(((path[-1][0][1],path[-1][0][1]+length),(path[-1][1][1],path[-1][1][1])))
    elif dir == 2:
        path.append(((path[-1][0][1],path[-1][0][1]),(path[-1][1][1],path[-1][1][1]-length)))
    elif dir == 0:
        path.append(((path[-1][0][1],path[-1][0][1]),(path[-1][1][1],path[-1][1][1]+length)))
        
path.pop(0)

digy = []
# (x,y,y)
for p in path:
    if p[1][0] == p[1][1]:
        digy.append((p[1][0],min(p[0][0],p[0][1]),max(p[0][0],p[0][1])))

digy = sorted(digy)

volume = 0
heights = [[digy[0][1],digy[0][2]]]

extravolume = 0
for di in range(1,len(digy)): 
    for height in heights:
        volume += (digy[di][0] - digy[di-1][0])*(height[1] - height[0]+1)
    hnchange = True
    for height in range(len(heights)):
        if hnchange:
            if digy[di][1] == heights[height][0]:
                if digy[di][2] == heights[height][1]:
                    # perfect stop
                    extravolume += heights[height][1]-heights[height][0]+1
                    heights.pop(height)
                    hnchange = False
                    break
                else:
                    # bottom height change
                    extravolume += digy[di][2]-heights[height][0]
                    heights[height][0] = digy[di][2]
                    thechange = [deepcopy(heights[height]),deepcopy(height)]
                    hnchange = False
            elif digy[di][2] == heights[height][1]:
                extravolume += heights[height][1] - digy[di][1]
                heights[height][1] = digy[di][1]
                thechange = [deepcopy(heights[height]),deepcopy(height)]
                hnchange = False
            elif digy[di][1] == heights[height][1]:
                heights[height][1] = digy[di][2]
                thechange = [deepcopy(heights[height]),deepcopy(height)]
                hnchange = False
            elif digy[di][2] == heights[height][0]:
                heights[height][0] = digy[di][1]
                thechange = [deepcopy(heights[height]),deepcopy(height)]
                hnchange = False 
            elif heights[height][0] < digy[di][1] < heights[height][1] and heights[height][0] < digy[di][2] < heights[height][1]:
                extravolume += digy[di][2] - digy[di][1] -1
                heights.append([digy[di][2],heights[height][1]])
                heights[height][1] = digy[di][1]
                break
      
    if hnchange == False:
        for height in range(len(heights)):
            # checks if two heights are clashing
            if thechange[0][0] == heights[height][1]:
                heights[height][1] = deepcopy(thechange[0][1])
                heights.pop(thechange[1])
                break
            elif thechange[0][1] == heights[height][0]:
                heights[height][0] = thechange[0][0]
                heights.pop(thechange[1])
                break
    if hnchange and height == len(heights)-1:
        heights.append([digy[di][1],digy[di][2]])

volume += extravolume
print(volume)
Leave a Comment