-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Recursion ... My brain managed to deal with it quite well for part 1 but part 2 | ||
# took me a really long time and I needed to get some inspiration from Reddit ... | ||
|
||
def load_numbers(path): | ||
return [int(i) for i in open(path).read().split()] | ||
|
||
def get_meta_sum(numbers, meta_sum=0): | ||
node_num = numbers.pop(0) | ||
meta_num = numbers.pop(0) | ||
|
||
for _ in range(node_num): | ||
meta_sum = get_meta_sum(numbers, meta_sum) | ||
|
||
for _ in range(meta_num): | ||
meta_sum += numbers.pop(0) | ||
|
||
return meta_sum | ||
|
||
def get_root_value(numbers): | ||
node_num = numbers.pop(0) | ||
meta_num = numbers.pop(0) | ||
|
||
values = [] | ||
for _ in range(node_num): | ||
value = get_root_value(numbers) | ||
values.append(value) | ||
|
||
meta = [] | ||
for _ in range(meta_num): | ||
value = numbers.pop(0) | ||
meta.append(value) | ||
|
||
if node_num == 0: | ||
return sum(meta) | ||
|
||
value = 0 | ||
for i in meta: | ||
index = i - 1 | ||
if index < node_num: | ||
value += values[index] | ||
|
||
return value | ||
|
||
# a | ||
numbers = load_numbers("inputs/08.txt") | ||
meta_sum = get_meta_sum(numbers) | ||
print(meta_sum) | ||
|
||
# b | ||
numbers = load_numbers("inputs/08.txt") | ||
root_value = get_root_value(numbers) | ||
print(root_value) | ||
|
||
|
||
# Tests: | ||
def test(): | ||
numbers = load_numbers("inputs/08_test.txt") | ||
assert get_meta_sum(numbers) == 138 | ||
numbers = load_numbers("inputs/08_test.txt") | ||
assert get_root_value(numbers) == 66 |
Oops, something went wrong.