Skip to content

Commit 10ce0f4

Browse files
committed
Added day 2018-08
1 parent 6fa48bf commit 10ce0f4

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

2018/08-Memory Maneuver.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2""",
8+
"expected": ['138', 'Unknown'],
9+
}
10+
11+
test = 'real'
12+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
13+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
14+
"expected": ['41849', '32487'],
15+
}
16+
17+
# -------------------------------- Control program execution -------------------------------- #
18+
19+
case_to_test = 'real'
20+
part_to_test = 2
21+
verbose_level = 1
22+
23+
# -------------------------------- Initialize some variables -------------------------------- #
24+
25+
puzzle_input = test_data[case_to_test]['input']
26+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
27+
puzzle_actual_result = 'Unknown'
28+
29+
30+
# -------------------------------- Actual code execution -------------------------------- #
31+
32+
nodes = {}
33+
node_hierarchy = {}
34+
35+
def process_node (data, i):
36+
global nodes, node_hierarchy
37+
parent = i
38+
subnodes, metadata = data[i:i+2]
39+
if subnodes == 0:
40+
nodes.update({i:data[i+2:i+2+metadata]})
41+
i += 2+metadata
42+
return i
43+
else:
44+
i += 2
45+
node_hierarchy[parent] = list()
46+
for j in range (subnodes):
47+
node_hierarchy[parent].append(i)
48+
i = process_node (data, i)
49+
nodes.update({parent:data[i:i+metadata]})
50+
i += metadata
51+
return i
52+
53+
def node_value (node, node_values):
54+
global nodes, node_hierarchy
55+
if node in node_values:
56+
return node_values[node]
57+
elif node not in node_hierarchy:
58+
return sum(nodes[node])
59+
else:
60+
children = [node_hierarchy[node][child-1] for child in nodes[node] if child <= len(node_hierarchy[node])]
61+
unknown_child_value = set(child for child in children if child not in node_values)
62+
if unknown_child_value:
63+
for child in unknown_child_value:
64+
node_values[child] = node_value(child, node_values)
65+
return sum(node_values[child] for child in children)
66+
67+
return node_values[node]
68+
69+
header = True
70+
71+
data = list(map(int, puzzle_input.split(' ')))
72+
process_node(data, 0)
73+
74+
if part_to_test == 1:
75+
puzzle_actual_result = sum(sum(nodes.values(), []))
76+
77+
else:
78+
puzzle_actual_result = node_value(0, {})
79+
80+
81+
82+
83+
# -------------------------------- Outputs / results -------------------------------- #
84+
85+
if verbose_level >= 3:
86+
print ('Input : ' + puzzle_input)
87+
print ('Expected result : ' + str(puzzle_expected_result))
88+
print ('Actual result : ' + str(puzzle_actual_result))
89+
90+
91+
92+

0 commit comments

Comments
 (0)