Browse Source

Minor day 7 tidying

master
Chris Smith 6 years ago
parent
commit
4b3459cc1e
1 changed files with 3 additions and 9 deletions
  1. 3
    9
      07.py

+ 3
- 9
07.py View File

@@ -4,13 +4,8 @@ programs = {}
4 4
 
5 5
 with open('data/07.txt', 'r') as file:
6 6
     for line in file.readlines():
7
-        if '->' in line:
8
-            details, children = line.strip().split(' -> ')
9
-            children = children.split(', ')
10
-        else:
11
-            details = line.strip()
12
-            children = []
13
-
7
+        details = line.strip().split(' -> ')[0]
8
+        children = line.strip().split(' -> ')[1].split(', ') if '->' in line else []
14 9
         name, weight = details.split(' ')
15 10
         weight = int(weight.strip('()'))
16 11
         programs[name] = SimpleNamespace(name=name, weight=weight, total_weight=-1, child_names=children, children=[])
@@ -26,8 +21,6 @@ for program in list(programs.values()):
26 21
     if len(program.children) == 0:
27 22
         leaves.append(program)
28 23
 
29
-print(f'Part one: {programs[roots[0]].name}')
30
-
31 24
 
32 25
 def resolve_weight(program):
33 26
     if program.total_weight == -1:
@@ -43,4 +36,5 @@ def check_weight(program):
43 36
 
44 37
 
45 38
 resolve_weight(programs[roots[0]])
39
+print(f'Part one: {programs[roots[0]].name}')
46 40
 print(f'Part two: {check_weight(programs[roots[0]])}')

Loading…
Cancel
Save