-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_code.py
More file actions
78 lines (63 loc) · 2.26 KB
/
Copy path07_code.py
File metadata and controls
78 lines (63 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import re
class Program:
def __init__(self, name, weight):
self.parent = None
self.name = name
self.weight = weight
self.disc = []
def add_to_disc(self, sub_tower):
sub_tower.set_parent(self)
self.disc.append(sub_tower)
def set_parent(self, parent):
self.parent = parent
def get_weight(self):
return self.weight
def get_subtowers(self):
return self.disc
def childrens_weights(self):
total_weights = 0
for child in self.disc:
total_weights += child.get_weight() + child.childrens_weights()
return total_weights
def __str__(self):
return self.name
def get_root(child):
if child.parent:
return get_root(child.parent)
return child
def recursive_circus(tower):
programs = {}
for program in tower:
if not program['name'] in programs:
programs[program['name']] = Program(program['name'], program['weight'])
tower = [x for x in tower if x['children']]
for program in tower:
for children in program['children'].split(', '):
programs[program['name']].add_to_disc(programs[children])
return get_root(programs[tower[0]['name']])
def part_2(root):
if not root.get_subtowers():
return -1
weights = []
for children in root.get_subtowers():
weights.append(children.get_weight() + children.childrens_weights())
if not max(weights) == min(weights):
if weights.count(min(weights)) > 1:
if part_2(root.get_subtowers()[weights.index(max(weights))]) == -1:
print root.get_subtowers()[weights.index(max(weights))].get_weight() - (max(weights) - min(weights))
else:
if part_2(root.get_subtowers()[weights.index(min(weights))]) == -1:
print root.get_subtowers()[weights.index(min(weights))].get_weight() + (max(weights) - min(weights))
else:
return -1
f = open('07_input.txt', 'r')
tower = []
pattern = re.compile('([a-z]+) \((\d+)\)( \-\> (.+))?')
for line in f.readlines():
found = pattern.search(line)
tower.append({'name': found.group(1), 'weight': int(found.group(2)), 'children': found.group(4)})
# Part 1
# root = recursive_circus(tower)
# print root
# Part 2
# part_2(root)