Guest User

Untitled

a guest
Dec 24th, 2021
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.63 KB | None | 0 0
  1. import sys
  2.  
  3. def var_index(var):
  4.     if var == "w":
  5.         return 0
  6.     if var == "x":
  7.         return 1
  8.     if var == "y":
  9.         return 2
  10.     if var == "z":
  11.         return 3
  12.     return -1
  13.  
  14. def var_value(variables, var):
  15.     if var == "w" or var == "x" or var == "y" or var == "z":
  16.         return variables[var_index(var)]
  17.     return var
  18.  
  19. def expression(index, value):
  20.     variable = [0] * 15
  21.     variable[index] = value
  22.     return variable
  23.  
  24. def min_value(variable):
  25.     result = variable[0]
  26.     for index in range(1, 15):
  27.         if variable[index] > 0:
  28.             result += variable[index]
  29.         else:
  30.             result += variable[index] * 9
  31.     return result
  32.  
  33. def max_value(variable):
  34.     result = variable[0]
  35.     for index in range(1, 15):
  36.         if variable[index] > 0:
  37.             result += variable[index] * 9
  38.         else:
  39.             result += variable[index]
  40.     return result
  41.  
  42. instructions = []
  43. input_data = open(r"c:\users\emurp\desktop\personal\aoc2021\24_input.txt")
  44. for input_line in input_data.read().splitlines():
  45.     input_line = input_line.replace("\n", "")
  46.     if input_line.startswith("inp"):
  47.         oper, var1_string = input_line.split(" ")
  48.         var2_string = ""
  49.     else:
  50.         oper, var1_string, var2_string = input_line.split(" ")
  51.     instruction = [oper, var1_string, var2_string]
  52.     instructions.append(instruction)
  53.  
  54. # Determine how much the final output actually depends on the 14-digit number
  55.  
  56. max_decision_count = 7
  57. for equality_decision in range(0, 2 ** max_decision_count):
  58.     # print ("Start of a pass")
  59.     decisions = []
  60.     current_equality_decision = equality_decision
  61.     decision_count = 0
  62.     variables = []
  63.     for index in range(0, 4):
  64.         variable = [0] * 15 # coefficients of 1, d1, d2, ..., d14
  65.         variables.append(variable)
  66.     model_digit = 0
  67.     for instruction in instructions:
  68.         # print ("Evaluating", instruction)
  69.         oper, var1, var2 = instruction
  70.         if oper == "inp":
  71.             model_digit += 1
  72.             variables[var_index(var1)] = expression(model_digit, 1)
  73.         else:
  74.             if var2.isnumeric() or (var2[0] == "-" and var2[1:].isnumeric()):
  75.                 expr = expression(0, int(var2))
  76.             else:
  77.                 expr = variables[var_index(var2)]
  78.             if oper == "add":
  79.                 for index in range(0, 15):
  80.                     variables[var_index(var1)][index] += expr[index]
  81.             elif oper == "mul":
  82.                 for index in range(1, 15):
  83.                     if expr[index] != 0:
  84.                         print ("Multiplication by non-constant expression")
  85.                         sys.exit()
  86.                 for index in range(0, 15):
  87.                     variables[var_index(var1)][index] *= expr[0]
  88.             elif oper == "div":
  89.                 for index in range(1, 15):
  90.                     if expr[index] != 0:
  91.                         print ("Division by non-constant expression")
  92.                         sys.exit()
  93.                 if expr[0] == 0:
  94.                     print ("Division by zero")
  95.                     sys.exit()
  96.                 for index in range(1, 15):
  97.                     if variables[var_index(var1)][index] % expr[0] > 0 and 9 * variables[var_index(var1)][index] >= expr[0]:
  98.                         print ("Division leading to fractional coefficient")
  99.                         sys.exit();
  100.                 for index in range(0, 15):
  101.                     numerator = variables[var_index(var1)][index]
  102.                     result = int(abs(numerator) / abs(expr[0]))
  103.                     if (numerator > 0 and expr[0] < 0) or (numerator < 0 and expr[0] > 0):
  104.                         result *= -1
  105.                     variables[var_index(var1)][index] = result
  106.             elif oper == "mod":
  107.                 for index in range(1, 15):
  108.                     if expr[index] != 0:
  109.                         print ("Mod by non-constant expression")
  110.                         sys.exit()
  111.                 if expr[0] <= 0:
  112.                     print ("Mod by", expr[0])
  113.                     sys.exit()
  114.                 for index in range(1, 15):
  115.                     if variables[var_index(var1)][index] % expr[0] > 0 and 9 * (variables[var_index(var1)][index] % expr[0]) >= expr[0]:
  116.                         print ("Mod failing to zero out or no-op a coefficient")
  117.                         sys.exit()
  118.                 for index in range(0, 15):
  119.                     variables[var_index(var1)][index] %= expr[0]
  120.             elif oper == "eql":
  121.                 min_val1 = min_value(variables[var_index(var1)])
  122.                 max_val1 = max_value(variables[var_index(var1)])
  123.                 min_val2 = min_value(expr)
  124.                 max_val2 = max_value(expr)
  125.                 if min_val1 > max_val2 or max_val1 < min_val2:
  126.                     variables[var_index(var1)] = expression(0, 0)
  127.                 else:
  128.                     needed_decision = False
  129.                     for index in range(1, 15):
  130.                         if variables[var_index(var1)][index] != expr[index]:
  131.                             if decision_count == max_decision_count:
  132.                                 print ("Max decision count exceeded")
  133.                                 sys.exit()
  134.                             decisions.append(variables[var_index(var1)])
  135.                             decisions.append(expr)
  136.                             if current_equality_decision % 2 == 1:
  137.                                 decisions.append("are equal")
  138.                                 variables[var_index(var1)] = expression(0, 1)
  139.                             else:
  140.                                 decisions.append("are not equal")
  141.                                 variables[var_index(var1)] = expression(0, 0)
  142.                             current_equality_decision /= 2
  143.                             decision_count += 1
  144.                             needed_decision = True
  145.                             break
  146.                     if not needed_decision:
  147.                         if variables[var_index(var1)][0] == expr[0]:
  148.                             variables[var_index(var1)] = expression(0, 1)
  149.                         else:
  150.                             variables[var_index(var1)] = expression(0, 0)
  151.         # print ("w =", variables[0])
  152.         # print ("x =", variables[1])
  153.         # print ("y =", variables[2])
  154.         # print ("z =", variables[3])
  155.     if min_value(variables[3]) <= 0 and max_value(variables[3]) >= 0:
  156.         print (decisions)
  157.         print (variables[3])
  158.         print ("**")
  159.     else:
  160.         print ("Final z value can never equal zero")
  161.         print (variables[3])
  162.         print ("--")
  163. print ("Reached end")
  164.  
Advertisement
Add Comment
Please, Sign In to add comment