51 lines
1.2 KiB
Python
Executable File
51 lines
1.2 KiB
Python
Executable File
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import copy
|
|
import json
|
|
|
|
#inputs = ["example.txt", "input.txt"]
|
|
#inputs = ["example.txt"]
|
|
inputs = ["input.txt"]
|
|
#inputs = ["n4z5Mtvk"]
|
|
|
|
|
|
class program(object):
|
|
def __init__(self, name, weight, children = []):
|
|
self.name = name
|
|
self.weight = weight
|
|
self.children = children
|
|
def toJSON(self):
|
|
return json.dumps(self, default=lambda o: o.__dict__,
|
|
sort_keys=True, indent=4)
|
|
def __repr__(self):
|
|
return self.toJSON()
|
|
|
|
|
|
def countlol(oix):
|
|
total = 0
|
|
total += oix.weight
|
|
for z in oix.children:
|
|
total += countlol(z)
|
|
return total
|
|
|
|
|
|
def solve(filename, item):
|
|
res = []
|
|
with open(filename, "r") as fp:
|
|
for line in fp:
|
|
name = line.split()[0]
|
|
value = int(line.split()[1].strip('()'))
|
|
if item == name:
|
|
if '->' in line:
|
|
for c in line.split('->')[1].split(','):
|
|
res.append(solve(filename, c.strip()))
|
|
return program(item, value, res)
|
|
|
|
|
|
for puzzle in inputs:
|
|
tree = solve(puzzle, 'rqwgj')
|
|
for c in tree.children[0].children[0].children:
|
|
print(c.name, countlol(c))
|
|
#print(puzzle, solve(puzzle, 'rqwgj'))
|