-
Notifications
You must be signed in to change notification settings - Fork 0
/
2019_06_a.py
43 lines (29 loc) · 931 Bytes
/
2019_06_a.py
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
from aocd.models import Puzzle
puzzle = Puzzle(year=2019, day=6)
input_data = puzzle.input_data
# input_data = "COM)B\nB)C\nC)D\nD)E\nE)F\nB)G\nG)H\nD)I\nE)J\nJ)K\nK)L"
class Node:
def __init__(self, child):
self.children = [child]
self.children_count = 1
def append_child(self, child):
self.children.append(child)
self.children_count += 1
tree = {}
for line in input_data.split('\n'):
parent, child = line.split(')')
if tree.get(parent):
tree[parent].append_child(child)
else:
tree[parent] = Node(child)
def get_children_count(node):
count_sum = node.children_count
for child in node.children:
if tree.get(child):
count_sum += get_children_count(tree[child])
return count_sum
children_sum = 0
for node in tree.values():
children_sum += get_children_count(node)
print(children_sum)
puzzle.answer_a = children_sum