This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
challange-12.py
195 lines (183 loc) · 5.47 KB
/
challange-12.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from functools import reduce
from itertools import chain
from json import dumps
from re import findall
def parse_rule(line):
# Unpack containing bag and it's contents from line
(_, container), *bags = findall(r'(\d*)\s*(\w+ \w+) bags?[\,\.]?', line)
return {
container: {},
**reduce(lambda graph, bag: {
**graph,
bag[1]: { container: int(bag[0]) }
} if bag[1] != "no other" else graph, bags, {})
}
def make_tree(graph, line):
rules = parse_rule(line)
return {
**graph,
**reduce( lambda update, key: {
**update,
key: {
**graph[key],
**rules[key]
} if key in graph else rules[key]
} , rules, {})
}
def count_ancestors(node, tree):
ancestors = []
count = 0
while tree[node] or len(ancestors[count:]):
ancestors += [key for key in tree[node] if key not in ancestors]
node = ancestors[count]
count += 1
return count
# Result
with open('./inputs/input-0G.txt') as f:
tree = reduce(make_tree, f, {})
print(count_ancestors('shiny gold', tree))
# Tests
with open('./inputs/test-0G.txt') as given:
tests = [
(
parse_rule('light red bags contain 1 bright white bag.'),
{
'light red': {},
'bright white': { 'light red': 1 }
},
"Cannot parse one bag"
),
(
parse_rule('light red bags contain 1 bright white bag, 2 muted yellow bags.'),
{
'light red': {},
'bright white': { 'light red': 1 },
'muted yellow': { 'light red': 2 }
},
"Cannot parse multiple bags"
),
(
parse_rule('faded blue bags contain no other bags.'),
{
'faded blue': {}
},
"Cannot parse orphaned leaf nodes"
),
(
reduce(make_tree, [
'light red bags contain 1 bright white bag.',
'dark orange bags contain 3 bright white bags.'
], {}),
{
'light red': {},
'dark orange': {},
'bright white': {
'light red': 1,
'dark orange': 3
}
},
"Cannot chart one bag across lines"
),
(
reduce(make_tree, [
'light red bags contain 1 bright white bag, 2 muted yellow bags.',
'dark orange bags contain 3 bright white bags, 4 muted yellow bags.'
], {}),
{
'light red': {},
'dark orange': {},
'bright white': {
'light red': 1,
'dark orange': 3
},
'muted yellow': {
'light red': 2,
'dark orange': 4
}
},
"Cannot chart multiple bags across lines"
),
(
reduce(make_tree, [
'vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.',
'faded blue bags contain no other bags.'
], {}),
{
'vibrant plum': {},
'faded blue': {
'vibrant plum': 5
},
'dotted black': {
'vibrant plum': 6
},
},
"Cannot chart empty bags across lines"
),
(
count_ancestors('bright white' ,{
'dark orange': {},
'bright white': {
'dark orange': 1
}
}),
1,
"Cannot count single ancestor"
),
(
count_ancestors('bright white' ,{
'dotted black': {},
'dark orange': {
'dotted black': 1
},
'bright white': {
'dark orange': 1
}
}),
2,
"Cannot count ancestor chain"
),
(
count_ancestors('bright white' ,{
'dotted black': {},
'faded blue': {},
'dark orange': {
'dotted black': 1,
'faded blue': 1
},
'bright white': {
'dark orange': 1
}
}),
3,
"Cannot count ancestor chain across branches"
),
(
count_ancestors('bright white' ,{
'faded blue': {},
'light gray': {
'faded blue': 1
},
'dark orange': {
'faded blue': 1
},
'bright white': {
'dark orange': 1,
'light gray': 1
}
}),
3,
"Cannot dedupe ancestors from chain"
),
(
count_ancestors('shiny gold', reduce(make_tree, given, {})),
4,
"Incorrect total sum"
)
]
for actual, expected, message in tests:
try:
assert dumps(actual, sort_keys=True) == dumps(expected, sort_keys=True), message
except AssertionError as e:
print(e)
print('Actual: ', actual)
print('Expected: ', expected)