Move python solutions to separate directory
This commit is contained in:
parent
47e97f4533
commit
3903907973
70 changed files with 0 additions and 0 deletions
1
py/2018/08/input.txt
Normal file
1
py/2018/08/input.txt
Normal file
File diff suppressed because one or more lines are too long
53
py/2018/08/solve.py
Normal file
53
py/2018/08/solve.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import sys
|
||||
|
||||
# PART 1
|
||||
|
||||
def load_stream(filename):
|
||||
with open(filename) as f:
|
||||
# Too lazy to do this properly
|
||||
return list(reversed(list(map(int, f.read()[:-1].split(" ")))))
|
||||
|
||||
def parse_node(stream):
|
||||
amt_nodes = stream.pop()
|
||||
amt_meta = stream.pop()
|
||||
|
||||
nodes = []
|
||||
for _ in range(amt_nodes):
|
||||
nodes.append(parse_node(stream))
|
||||
|
||||
meta = []
|
||||
for _ in range(amt_meta):
|
||||
meta.append(stream.pop())
|
||||
|
||||
return (nodes, meta)
|
||||
|
||||
def sum_of_meta(node):
|
||||
nodes, meta = node
|
||||
return sum(meta) + sum(map(sum_of_meta, nodes))
|
||||
|
||||
# PART 2
|
||||
|
||||
def value_of(node):
|
||||
nodes, meta = node
|
||||
if nodes:
|
||||
total = 0
|
||||
for i in meta:
|
||||
i -= 1
|
||||
if i >= 0 and i < len(nodes):
|
||||
total += value_of(nodes[i])
|
||||
return total
|
||||
else:
|
||||
return sum(meta)
|
||||
|
||||
def main(filename):
|
||||
print(f"Solutions for {filename}")
|
||||
stream = load_stream(filename)
|
||||
node = parse_node(stream)
|
||||
meta = sum_of_meta(node)
|
||||
print(f"Part 1: {meta}")
|
||||
value = value_of(node)
|
||||
print(f"Part 2: {value}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
for filename in sys.argv[1:]:
|
||||
main(filename)
|
||||
1
py/2018/08/test_input.txt
Normal file
1
py/2018/08/test_input.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2
|
||||
Loading…
Add table
Add a link
Reference in a new issue