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

Loading…
Cancel
Save