Advertisement
Guest User

Untitled

a guest
Dec 21st, 2022
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. import os, sys, math, functools, json, copy
  2. INFILE = sys.argv[1] if len(sys.argv)>1 else "input.txt"
  3. rawlines = []
  4. with open(INFILE, "r") as f:
  5.     rawlines = f.readlines()
  6. lines = []
  7. for l in rawlines:
  8.     lines.append(l.strip())
  9. print(len(lines),"lines input from",INFILE)
  10. #########################
  11.  
  12. monkeys = []
  13.  
  14. for line in lines:
  15.     thisName = line.split(":")[0]
  16.     thisOperation = line.split(": ")[1]
  17.     thisMulti = " * " in thisOperation
  18.     thisDivide = " / " in thisOperation
  19.     thisAdd = " + " in thisOperation
  20.     thisSubtract = " - " in thisOperation
  21.     thisVal = None
  22.     thisTarget1 = False
  23.     thisTarget2 = False
  24.  
  25.     if not thisMulti and not thisDivide and not thisAdd and not thisSubtract:
  26.         thisVal = int(thisOperation)
  27.     else:
  28.         thisTarget1 = thisOperation.split(" ")[0]
  29.         thisTarget2 = thisOperation.split(" ")[2]
  30.  
  31.     monkey = {
  32.         "name": thisName,
  33.         "value": thisVal,
  34.         "multiply": thisMulti,
  35.         "divide": thisDivide,
  36.         "add": thisAdd,
  37.         "subtract": thisSubtract,
  38.         "target1": thisTarget1,
  39.         "target2": thisTarget2
  40.     }
  41.     monkeys.append(monkey)
  42.  
  43. def getMonkeyIdx(monkeyname) -> int:
  44.     for x in range(len(monkeys)):
  45.         if monkeys[x]["name"] == monkeyname:
  46.             return x
  47.     assert False
  48.  
  49. def getMonkeyVal(monkeyname):
  50.     monkeyIdx = getMonkeyIdx(monkeyname)
  51.     M = monkeys[monkeyIdx]
  52.     if M["value"] is not None:
  53.         return M["value"]
  54.    
  55.     if M["multiply"]:
  56.         return getMonkeyVal(M["target1"]) * getMonkeyVal(M["target2"])
  57.  
  58.     if M["divide"]:
  59.         return getMonkeyVal(M["target1"]) / getMonkeyVal(M["target2"])
  60.  
  61.     if M["add"]:
  62.         return getMonkeyVal(M["target1"]) + getMonkeyVal(M["target2"])
  63.  
  64.     if M["subtract"]:
  65.         return getMonkeyVal(M["target1"]) - getMonkeyVal(M["target2"])
  66.  
  67.  
  68. print("part 1:", int(getMonkeyVal("root"))) # 54703080378102
  69.  
  70. humanMonkeyIdx = getMonkeyIdx("humn")
  71. rootTarget1 = monkeys[getMonkeyIdx("root")]["target1"]
  72. rootTarget2 = monkeys[getMonkeyIdx("root")]["target2"]
  73. x = 0
  74. while True:
  75.     monkeys[humanMonkeyIdx]["value"] = x
  76.     diff = abs(getMonkeyVal(rootTarget1) - getMonkeyVal(rootTarget2))
  77.     #print(x, "=", diff)
  78.     if diff == 0:
  79.         print("part 2:", x)
  80.         sys.exit(1)
  81.     else:
  82.         x += max(int(diff*0.1), 1) # this is sketchy (going past 0.1 causes negative)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement