Advertisement
SimonJkAdamek

AOC day 7

Dec 8th, 2022 (edited)
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | Source Code | 0 0
  1. import re
  2.  
  3. data=dict()
  4. act_key=""
  5. same_c=[]
  6. same_l=[]
  7.  
  8. with open("input.txt") as file:
  9.     for line in file:
  10.         n=1
  11.         while re.match("^dir.*",line) and line.split()[1] in same_l:
  12.             x=line.split()
  13.             x[1]=x[1]+str(n)
  14.             line=" ".join(x)
  15.  
  16.         while re.match("^\$\scd\s[^\.].*",line) and line.split()[2] in same_c:
  17.             x=line.split()
  18.             x[2]=x[2]+str(n)
  19.             line=" ".join(x)
  20.  
  21.         if re.match("^\$\scd\s[^\.].*",line):
  22.             act_key=line.split()[2]
  23.             data[act_key]=[0]
  24.             same_c.append(line.split()[2])
  25.         elif re.match("^[^\$].*",line):
  26.             if line.split()[0].isnumeric():
  27.                 data[act_key][0]+=int(line.split()[0])
  28.             else:
  29.                 data[act_key].append(line.split()[1])
  30.                 same_l.append(line.split()[1])
  31.  
  32. for i in list(data.keys())[::-1]:
  33.     if len(data[i])>1:
  34.         for j in range(len(data[i][1:])):
  35.             subdir=data[i][j+1]
  36.             data[i][0]+=int(data[subdir][0])
  37.        
  38.         del(data[i][1:])
  39.  
  40. print("Part 1:",sum([size[0] for size in data.values() if size[0]<=100000]))
  41. print("Part 2:", sorted([data[size][0] for size in data.keys() if 70000000-data["/"][0]+data[size][0]>=30000000])[0])
  42.  
  43.  
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement