You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB

lines = open("input.txt", "r").read().splitlines()
class Monkey:
def __init__(self, name=None, number=None, childA=None, childB=None, op=None):
self.name = name
self.number = number
self.childA = childA
self.childB = childB
self.op = op
def yell(self):
if self.number != None:
return self.number
else:
if self.op == "+":
return monkeys[self.childA].yell() + monkeys[self.childB].yell()
elif self.op == "-":
return monkeys[self.childA].yell() - monkeys[self.childB].yell()
elif self.op == "*":
return monkeys[self.childA].yell() * monkeys[self.childB].yell()
elif self.op == "/":
return monkeys[self.childA].yell() / monkeys[self.childB].yell()
monkeys = {}
for line in lines:
print(line)
name = line.split(" ")[0][:-1]
rest = line.split(" ")[1:]
if len(rest) == 1:
monkeys[name] = Monkey(name=name, number=int(rest[0]))
else:
childA = rest[0]
childB = rest[2]
op = rest[1]
monkeys[name] = Monkey(name=name, childA=childA, childB=childB, op=op)
print(int(monkeys["root"].yell()))