added next to days
This commit is contained in:
1
08/input.txt
Normal file
1
08/input.txt
Normal file
File diff suppressed because one or more lines are too long
2
08/input_sample.txt
Normal file
2
08/input_sample.txt
Normal file
@ -0,0 +1,2 @@
|
||||
2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2
|
||||
|
||||
26
08/solve01.py
Executable file
26
08/solve01.py
Executable file
@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
|
||||
def read_input():
|
||||
with open('input.txt', 'r') as f:
|
||||
data = f.read().splitlines()
|
||||
return [ int(x) for x in data[0].split(" ")]
|
||||
|
||||
|
||||
def get_node_meta(data, pos):
|
||||
num_ch = data[pos]
|
||||
num_m = data[pos+1]
|
||||
len_ch = 0 # total length of children
|
||||
meta = []
|
||||
|
||||
for ch in range(num_ch):
|
||||
(ch_len, ch_meta) = get_node_meta(data, pos+2+len_ch)
|
||||
len_ch += ch_len
|
||||
meta = meta + ch_meta
|
||||
|
||||
meta = meta + data[pos+2+len_ch:pos+2+len_ch+num_m]
|
||||
return (len_ch+2+num_m, meta)
|
||||
|
||||
tree = read_input()
|
||||
(_, meta) = get_node_meta(tree, 0)
|
||||
print(sum(meta))
|
||||
35
08/solve02.py
Executable file
35
08/solve02.py
Executable file
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
|
||||
def read_input():
|
||||
with open('input.txt', 'r') as f:
|
||||
data = f.read().splitlines()
|
||||
return [ int(x) for x in data[0].split(" ")]
|
||||
|
||||
|
||||
def get_node_meta(data, pos):
|
||||
num_ch = data[pos]
|
||||
num_m = data[pos+1]
|
||||
len_ch = 0 # total length of children
|
||||
values = [0] * num_ch
|
||||
value = 0
|
||||
|
||||
for ch in range(num_ch):
|
||||
(ch_len, ch_value) = get_node_meta(data, pos+2+len_ch)
|
||||
len_ch += ch_len
|
||||
values[ch] = ch_value
|
||||
|
||||
meta = data[pos+2+len_ch:pos+2+len_ch+num_m]
|
||||
|
||||
# do not have children
|
||||
if num_ch == 0:
|
||||
value = sum(meta)
|
||||
else:
|
||||
for m in meta:
|
||||
if m <= num_ch:
|
||||
value += values[m-1]
|
||||
return (len_ch+2+num_m, value)
|
||||
|
||||
tree = read_input()
|
||||
(_, value) = get_node_meta(tree, 0)
|
||||
print(value)
|
||||
Reference in New Issue
Block a user