-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent_7_1.py
54 lines (42 loc) · 1.26 KB
/
advent_7_1.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
43
44
45
46
47
48
49
50
51
52
53
54
import requests
import re
class Node:
def __init__(self, name, weight):
self.name = name
self.weight = weight
self.children = []
def __str__(self):
return '%s (%s)' % (self.name, self.weight)
def print_tree(self, level = 0):
print '\t' * level, self
for child in self.children:
child.print_tree(level + 1)
def find_node(name, node):
if name == node.name:
return node
else:
for child in node.children:
node = find_node(name, child)
if node:
return node
cookie = {'session': '53616c7465645f5f9531a1cfa48d35689f93c460fb25bcd376e0bfaeea195562d7ce6e82497e0972b6fd4e408f5c176f'}
r = requests.get('https://adventofcode.com/2017/day/7/input', cookies = cookie)
lines = r.text.strip().split('\n')
root = Node('root', 0)
for line in lines:
name, weight = re.match('(\w+) .(\d+).', line).groups()
node = Node(name, weight)
root.children.append(node)
for line in lines:
name = re.match('(\w+) .(\d+).', line).groups()[0]
parent = find_node(name, root)
has_children = '->' in line
if has_children:
children = line[line.index('-> ') + 2:].split(',')
for child in children:
child = child.strip()
for node in root.children:
if child == node.name:
root.children.remove(node)
parent.children.append(node)
root.print_tree()