-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7a.py
More file actions
96 lines (84 loc) · 3.43 KB
/
7a.py
File metadata and controls
96 lines (84 loc) · 3.43 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#Advent of code
# 01/21/21 (start 12/14/2020) day7 7a
import re
# pip3 install treelib
from treelib import Node, Tree
filename = "data7.txt"
file = open(filename)
filestr = file.read()
a_list = filestr.split("\n")
maxindex = len(a_list)
print(a_list)
print(f"maxindex={maxindex}, maxcolumns={len(a_list[0])}")
#muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
#plaid coral bags contain 2 pale green bags, 2 faded tomato bags, 2 dark salmon bags, 1 vibrant magenta bag.
#faded blue bags contain no other bags.
# parse the file (try regex)
# make list of lists, to show connections.
# go backwards from the shiny gold bags.
# can make an initial test case parsing and finding the top level holders of shiny gold.
tree = Tree()
tree.create_node("root", "root") # create root, (tag, identifier)
#tree.create_node("noparent", "noparent",parent="root")
#tree.create_node("shiny gold", "shiny gold", parent="root") # create root, (tag, identifier)
tree.show()
for line in a_list:
# TODO: parse the comma(s) afterwards for other groups!
m = re.match(r"(\w+ \w+) (?:bags) (?:contain) (\w+) (\w+ \w+) (?:bags*)", line)
# grpall = m.group(0)
# print(f"{grpall}") # string
print(f"{m.groups()}") # string in groups, indexed 1 and up.
#print(f"len={len(grpall)}, span={m.span()}")
par = m.group(1) # first entry
child = m.group(3)
child_list = []
child_list.append(child)
cnt_other = line.count(',')
print(f"cnt_other={cnt_other}")
if cnt_other > 0:
remain_list = line.split(", ")
remain_list.pop(0) # drop already processed child and parent string entry
print(f"remain_list={remain_list}")
for i in range(0, cnt_other):
m = re.match(r"(\w+) (\w+ \w+) (?:bags*)", remain_list[i])
num = m.group(1)
child = m.group(2)
print(f"child={child}")
child_list.append(child)
# if new, create parent node (parent of root temporarily.)
# if not new, use it.
# then similar for children.
if not tree.contains(par):
# new so create the parent (and make its parent root temporarily OR leave it as None for now???)
tree.create_node(par, par, parent="root")
for child in child_list:
if not tree.contains(child):
tree.create_node(child, child, parent=par)
else:
# reparent the child, since child already exists (assert that parent was previously root)
#tree.create_node(child, child, parent="root")
#node = tree.get_node(child)
prev_par = tree.parent(child)
print(f"!!! previous parent = {prev_par} !!!")
tree.move_node(child, par)
# for child in child_list:
# if tree.contains(par):
# tree.create_node(child, child, parent=par)
# else:
# # add to root until we can find its true parent (later)
# tree.create_node(child, child, parent="root")
tree.show()
# count = 0
# for c in m.groups():
# #print(f"{c}")
# count = count +1
# print(f"count = {count}")
tree.show()
# the group is as shown afterwards: (child, count, THEN parent)
# shiny gold is the parent.
# vibrant plum bags contain 5 faded blue bag, 6 dotted black bags.
# ('vibrant plum', '5', 'faded blue')
# >>> import re # split, search, match
# >>> m = re.search('(?<=abc)def', 'abcdef')
# >>> m.group(0)
#m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")