Advertisement
desrtfx

Day21_Part01.py

Dec 21st, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. operators = {'+': lambda a, b: a + b,
  2.              '-': lambda a, b: a - b,
  3.              '*': lambda a, b: a * b,
  4.              '/': lambda a, b: a // b,
  5.              }
  6.  
  7. with open("Input_Day21.txt") as f:
  8.     raw_data = [x.strip() for x in f]
  9.  
  10. d = []
  11. for row in raw_data:
  12.     id, op = row.split(": ")
  13.     d.append((id, op))
  14.  
  15. monkeys = {}
  16. while 'root' not in monkeys:
  17.     for _m,_d in d:
  18.         if _m in monkeys:
  19.             continue
  20.         if _d.isnumeric():
  21.             monkeys[_m] = int(_d)
  22.         else:
  23.             m1, op, m2 = _d.split(' ')
  24.             if (m1 in monkeys) and (m2 in monkeys):
  25.                 monkeys[_m] = operators[op](monkeys[m1], monkeys[m2])
  26. print(monkeys['root'])
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement