summaryrefslogtreecommitdiff
path: root/2022/day07.py
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2022-12-11 14:47:27 +0100
committernekineki <nekineki@nekineki.net>2022-12-11 14:50:32 +0100
commitfe75c10e350743a1c078f065d69556fecf825ca5 (patch)
tree956556a564c9329346dc9d8b1535dc5e365f3d55 /2022/day07.py
parenta74d2dc54aef546664bcc8c81eb8e01a93e94391 (diff)
move files around, update paths
Diffstat (limited to '2022/day07.py')
-rwxr-xr-x2022/day07.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/2022/day07.py b/2022/day07.py
new file mode 100755
index 0000000..7e3f593
--- /dev/null
+++ b/2022/day07.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python3
+
+# import numpy as np
+from functools import reduce
+from re import findall
+from copy import deepcopy
+import sys
+
+# filename = "in/day07.ref"
+filename = "in/day07.pzl"
+data = open(filename).read()
+lines = [line for line in data.rstrip('\n').split('\n')]
+
+res1 = 0
+res2 = 0
+
+pwd = '/'
+fs = {'files':0}
+
+for line in lines:
+
+ l = line.split(' ')
+
+ if l[0] == '$':
+ if l[1] == 'cd':
+ if l[2] == '/':
+ pwd = '/'
+ elif l[2] == '..':
+ pwd = pwd[: -1 -pwd[:-1][::-1].find('/') ]
+ else:
+ pwd = pwd + l[2] + '/'
+
+ # print(line)
+ # print(pwd)
+
+ p = pwd.strip('/').split('/')
+ if p != ['']:
+ fsp = fs
+ for i in p:
+ if i not in fsp.keys():
+ fsp[i] = {'files':0}
+ fsp = fsp[i]
+
+ elif l[1] == 'ls':
+ continue
+
+ else:
+ if l[0] == 'dir':
+ continue
+ size, name = int(l[0]), l[1]
+
+ p = pwd.strip('/').split('/')
+ if p != ['']:
+ fsp = fs
+ for i in p:
+ fsp = fsp[i]
+ fsp['files'] += size
+ else:
+ fs['files'] += size
+
+
+def print_fs(subfs, depth = 0):
+ for d in subfs:
+ if d == 'files':
+ print(depth * ' ', d, ' ', subfs[d], sep = '')
+ else:
+ print(depth * ' ', d, sep = '')
+ print_fs(subfs[d], depth+1)
+
+def rec_fs(subfs):
+ size = 0
+ for d in subfs:
+ if d == 'files':
+ size += subfs[d]
+ global sizes_one
+ sizes_one.append(subfs[d])
+ # print(d, size)
+ else:
+ size += rec_fs(subfs[d])
+
+ global sizes_cumulative
+ sizes_cumulative.append(size)
+
+ return size
+
+sizes_one = list()
+sizes_cumulative = list()
+
+# print(fs)
+# print_fs(fs)
+
+rec_fs(fs)
+
+total_size = 70000000
+needed_size = 30000000
+used_size = sum(sizes_one)
+delete_size = needed_size -(total_size - used_size)
+total_size - used_size
+
+res1 = sum(filter(lambda x: x <= 100000, sizes_cumulative))
+res2 = list(filter(lambda x: x > delete_size, sorted(sizes_cumulative)))[0]
+
+print('res1:', res1)
+print('res2:', res2)
+