zNebula

Advent of Code Day 7 Part 1

Dec 7th, 2022
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | Source Code | 0 0
  1. with open("input.txt") as readFile:
  2.     lineLst = []
  3.     for lines in readFile:
  4.         lineLst.append(lines.strip())
  5. readFile.close()
  6.  
  7. fileSystem = {}
  8. for line in lineLst:
  9.     if line.startswith('$'):
  10.         if line.startswith('$ cd'):
  11.             if line[5:] == '..':
  12.                 continue
  13.             else:
  14.                 recentDir = line[5:]
  15.                 fileSystem[recentDir] = []
  16.         else:
  17.             continue
  18.     else:
  19.         if line.startswith('dir'):
  20.             fileSystem[recentDir].append(line)
  21.         else:
  22.             fileSystem[recentDir].append(line.split(' ')[0])
  23.  
  24. sums = []
  25. for key in fileSystem:
  26.     while any(file.startswith('dir') for file in fileSystem[key]):
  27.               for i,files in enumerate(fileSystem[key]):
  28.                   if files.startswith('dir'):
  29.                       fileSystem[key].extend(fileSystem[files[4:]])
  30.                       fileSystem[key].pop(i)
  31.     sums.append(sum(list(map(int,fileSystem[key]))))
  32.  
  33. sumsLess = []
  34.  
  35. for dirSums in sums:
  36.     if dirSums <= 100000:
  37.         sumsLess.append(dirSums)
  38.        
  39. print(sum(sumsLess))
  40.                  
  41.  
Advertisement
Add Comment
Please, Sign In to add comment