Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- def var_index(var):
- if var == "w":
- return 0
- if var == "x":
- return 1
- if var == "y":
- return 2
- if var == "z":
- return 3
- return -1
- def var_value(variables, var):
- if var == "w" or var == "x" or var == "y" or var == "z":
- return variables[var_index(var)]
- return var
- def expression(index, value):
- variable = [0] * 15
- variable[index] = value
- return variable
- def min_value(variable):
- result = variable[0]
- for index in range(1, 15):
- if variable[index] > 0:
- result += variable[index]
- else:
- result += variable[index] * 9
- return result
- def max_value(variable):
- result = variable[0]
- for index in range(1, 15):
- if variable[index] > 0:
- result += variable[index] * 9
- else:
- result += variable[index]
- return result
- instructions = []
- input_data = open(r"c:\users\emurp\desktop\personal\aoc2021\24_input.txt")
- for input_line in input_data.read().splitlines():
- input_line = input_line.replace("\n", "")
- if input_line.startswith("inp"):
- oper, var1_string = input_line.split(" ")
- var2_string = ""
- else:
- oper, var1_string, var2_string = input_line.split(" ")
- instruction = [oper, var1_string, var2_string]
- instructions.append(instruction)
- # Determine how much the final output actually depends on the 14-digit number
- max_decision_count = 7
- for equality_decision in range(0, 2 ** max_decision_count):
- # print ("Start of a pass")
- decisions = []
- current_equality_decision = equality_decision
- decision_count = 0
- variables = []
- for index in range(0, 4):
- variable = [0] * 15 # coefficients of 1, d1, d2, ..., d14
- variables.append(variable)
- model_digit = 0
- for instruction in instructions:
- # print ("Evaluating", instruction)
- oper, var1, var2 = instruction
- if oper == "inp":
- model_digit += 1
- variables[var_index(var1)] = expression(model_digit, 1)
- else:
- if var2.isnumeric() or (var2[0] == "-" and var2[1:].isnumeric()):
- expr = expression(0, int(var2))
- else:
- expr = variables[var_index(var2)]
- if oper == "add":
- for index in range(0, 15):
- variables[var_index(var1)][index] += expr[index]
- elif oper == "mul":
- for index in range(1, 15):
- if expr[index] != 0:
- print ("Multiplication by non-constant expression")
- sys.exit()
- for index in range(0, 15):
- variables[var_index(var1)][index] *= expr[0]
- elif oper == "div":
- for index in range(1, 15):
- if expr[index] != 0:
- print ("Division by non-constant expression")
- sys.exit()
- if expr[0] == 0:
- print ("Division by zero")
- sys.exit()
- for index in range(1, 15):
- if variables[var_index(var1)][index] % expr[0] > 0 and 9 * variables[var_index(var1)][index] >= expr[0]:
- print ("Division leading to fractional coefficient")
- sys.exit();
- for index in range(0, 15):
- numerator = variables[var_index(var1)][index]
- result = int(abs(numerator) / abs(expr[0]))
- if (numerator > 0 and expr[0] < 0) or (numerator < 0 and expr[0] > 0):
- result *= -1
- variables[var_index(var1)][index] = result
- elif oper == "mod":
- for index in range(1, 15):
- if expr[index] != 0:
- print ("Mod by non-constant expression")
- sys.exit()
- if expr[0] <= 0:
- print ("Mod by", expr[0])
- sys.exit()
- for index in range(1, 15):
- if variables[var_index(var1)][index] % expr[0] > 0 and 9 * (variables[var_index(var1)][index] % expr[0]) >= expr[0]:
- print ("Mod failing to zero out or no-op a coefficient")
- sys.exit()
- for index in range(0, 15):
- variables[var_index(var1)][index] %= expr[0]
- elif oper == "eql":
- min_val1 = min_value(variables[var_index(var1)])
- max_val1 = max_value(variables[var_index(var1)])
- min_val2 = min_value(expr)
- max_val2 = max_value(expr)
- if min_val1 > max_val2 or max_val1 < min_val2:
- variables[var_index(var1)] = expression(0, 0)
- else:
- needed_decision = False
- for index in range(1, 15):
- if variables[var_index(var1)][index] != expr[index]:
- if decision_count == max_decision_count:
- print ("Max decision count exceeded")
- sys.exit()
- decisions.append(variables[var_index(var1)])
- decisions.append(expr)
- if current_equality_decision % 2 == 1:
- decisions.append("are equal")
- variables[var_index(var1)] = expression(0, 1)
- else:
- decisions.append("are not equal")
- variables[var_index(var1)] = expression(0, 0)
- current_equality_decision /= 2
- decision_count += 1
- needed_decision = True
- break
- if not needed_decision:
- if variables[var_index(var1)][0] == expr[0]:
- variables[var_index(var1)] = expression(0, 1)
- else:
- variables[var_index(var1)] = expression(0, 0)
- # print ("w =", variables[0])
- # print ("x =", variables[1])
- # print ("y =", variables[2])
- # print ("z =", variables[3])
- if min_value(variables[3]) <= 0 and max_value(variables[3]) >= 0:
- print (decisions)
- print (variables[3])
- print ("**")
- else:
- print ("Final z value can never equal zero")
- print (variables[3])
- print ("--")
- print ("Reached end")
Advertisement
Add Comment
Please, Sign In to add comment