Add day 7 solutions - GHETTO
This commit is contained in:
parent
4d1fabe573
commit
e56615e7ee
|
@ -0,0 +1,97 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import copy
|
||||||
|
import json
|
||||||
|
|
||||||
|
#inputs = ["example.txt", "input.txt"]
|
||||||
|
#inputs = ["example.txt"]
|
||||||
|
inputs = ["input.txt"]
|
||||||
|
#inputs = ["n4z5Mtvk"]
|
||||||
|
|
||||||
|
def countlol(oix):
|
||||||
|
total = 0
|
||||||
|
total += oix.weight
|
||||||
|
for z in oix.children:
|
||||||
|
total += countlol(z)
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
class program(object):
|
||||||
|
def __init__(self, name, weight, children = []):
|
||||||
|
self.name = name
|
||||||
|
self.weight = int(weight.replace('(', '').replace(')', ''))
|
||||||
|
self.children = children
|
||||||
|
def toJSON(self):
|
||||||
|
return json.dumps(self, default=lambda o: o.__dict__,
|
||||||
|
sort_keys=True, indent=4)
|
||||||
|
def __repr__(self):
|
||||||
|
return str(self.__dict__)
|
||||||
|
|
||||||
|
def search(programs, name):
|
||||||
|
for idx, val in enumerate(programs):
|
||||||
|
if val.name == name:
|
||||||
|
return idx
|
||||||
|
|
||||||
|
def solve(filename):
|
||||||
|
programs = []
|
||||||
|
tree = []
|
||||||
|
|
||||||
|
# read all items first
|
||||||
|
with open(filename, "r") as fp:
|
||||||
|
for line in fp:
|
||||||
|
val = line.strip().split(" ")
|
||||||
|
programs.append(program(val[0], val[1]))
|
||||||
|
|
||||||
|
# create tree
|
||||||
|
with open(filename, "r") as fp:
|
||||||
|
for line in fp:
|
||||||
|
val = line.strip().split(" ")
|
||||||
|
if len(val) > 2:
|
||||||
|
childs = []
|
||||||
|
for idx in range(3, len(val)):
|
||||||
|
name = val[idx].replace(',', '')
|
||||||
|
pidx = search(programs, name)
|
||||||
|
childs.append(copy.deepcopy(programs[pidx]))
|
||||||
|
del programs[pidx]
|
||||||
|
tree.append(program(val[0], val[1], childs))
|
||||||
|
|
||||||
|
indexes = {}
|
||||||
|
delete = []
|
||||||
|
|
||||||
|
#print(tree)
|
||||||
|
|
||||||
|
for idx, val in enumerate(tree):
|
||||||
|
indexes[val.name] = idx
|
||||||
|
|
||||||
|
for idx, val in enumerate(tree):
|
||||||
|
for cidx, child in enumerate(val.children):
|
||||||
|
if child.name in indexes:
|
||||||
|
tree[idx].children[cidx] = copy.deepcopy(tree[indexes[child.name]])
|
||||||
|
delete.append(indexes[child.name])
|
||||||
|
#del indexes[child.name]
|
||||||
|
|
||||||
|
#print(len(tree))
|
||||||
|
|
||||||
|
delete.sort(reverse=True)
|
||||||
|
for idx in delete:
|
||||||
|
del tree[idx]
|
||||||
|
|
||||||
|
#print(tree[0].toJSON())
|
||||||
|
|
||||||
|
#print(len(tree))
|
||||||
|
|
||||||
|
for c in tree[0].children:
|
||||||
|
for c2 in c.children:
|
||||||
|
print(c2.name, countlol(c2))
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return programs
|
||||||
|
|
||||||
|
|
||||||
|
for puzzle in inputs:
|
||||||
|
print(puzzle, solve(puzzle))
|
|
@ -0,0 +1,55 @@
|
||||||
|
#!/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 total(self):
|
||||||
|
result = self.weight
|
||||||
|
for child in self.children:
|
||||||
|
result += self.total(child)
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
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'))
|
|
@ -0,0 +1,13 @@
|
||||||
|
pbga (66)
|
||||||
|
xhth (57)
|
||||||
|
ebii (61)
|
||||||
|
havc (66)
|
||||||
|
ktlj (57)
|
||||||
|
fwft (72) -> ktlj, cntj, xhth
|
||||||
|
qoyq (66)
|
||||||
|
padx (45) -> pbga, havc, qoyq
|
||||||
|
tknk (41) -> ugml, padx, fwft
|
||||||
|
jptl (61)
|
||||||
|
ugml (68) -> gyxo, ebii, jptl
|
||||||
|
gyxo (61)
|
||||||
|
cntj (57)
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue