main.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import os, sys, re
  2. task_dir = os.path.dirname(__file__)
  3. sys.path.append(f'{task_dir}/..')
  4. from get_tasks import get_input, generate_readme, check_example
  5. def ALU(program, number):
  6. number = iter(map(int, str(number)))
  7. ma = {'w':0, 'z':0, 'x':0, 'y':0}
  8. for idx, line in enumerate(program):
  9. if len(inp := line.split()) == 2: ma[inp[1]] = next(number)
  10. else:
  11. ins, a, b = line.split()
  12. if ins == 'add': ma[a] = ma[a] + int(b) if b.isdigit() or b.startswith('-') else ma[a] + ma[b]
  13. if ins == 'mul': ma[a] = ma[a] * int(b) if b.isdigit() or b.startswith('-') else ma[a] * ma[b]
  14. if ins == 'mod':
  15. if ma[a] < 0 : raise ValueError(f'{a} < 0! or {b} <= 0: {ma[a]}. istruction number {idx}: {line}')
  16. else: ma[a] = ma[a] % int(b) if b.isdigit() else ma[a] % ma[b]
  17. if ins == 'div':
  18. if (b.isdigit() and int(b) < 0) or (not b.isdigit() and ma[b] < 0):
  19. raise ValueError(f'{b} <= 0: {b if b.isdigit() else ma[b]}. istruction number {idx}: {line}')
  20. else: ma[a] = int(ma[a] / int(b)) if b.isdigit() or b.startswith('-') else int((ma[a] / ma[b]))
  21. if ins == 'eql':
  22. if b.isdigit() and ma[a] == int(b):
  23. ma[a] = 1
  24. elif not b.isdigit() and ma[b] == ma[a]:
  25. ma[a] = 1
  26. else:
  27. ma[a] = 0
  28. return ma['w'], ma['x'], ma['y'], ma['z']
  29. def get_equations_from_input(input):
  30. placeholder = 'abcdefghijklmn'
  31. number = iter(placeholder)
  32. pstack = []
  33. diffs = []
  34. stack = []
  35. equations = []
  36. for idx, line in enumerate(input):
  37. match idx % 18:
  38. case 5 | 15: diffs.append(line)
  39. for idx, line in enumerate(diffs):
  40. _, a, b = line.split()
  41. if a == 'x' and b.startswith("-"):
  42. pstack.append(['pop', f'{b} = {next(number)}'])
  43. elif a == 'x':
  44. pstack.append(['push', f'{next(number)} + {diffs[idx+1].split()[2]}'])
  45. for line in pstack:
  46. if line[0] == 'push':
  47. stack.append(line[1])
  48. elif line[0] == 'pop':
  49. pop = stack.pop().split('+')
  50. val = line[1].split('=')
  51. equations.append([f"{pop[0]} + {eval(pop[-1]+val[0])} = {val[-1]}"])
  52. return equations
  53. def part1(equations):
  54. placeholder = 'abcdefghijklmn'
  55. for equation in equations:
  56. if re.findall(r"\-", equation[0]):
  57. equation = equation[0]
  58. letter1 = equation[0]
  59. letter2 = equation[-1]
  60. placeholder = placeholder.replace(letter1, '9')
  61. equation = equation.replace(letter1, '9')
  62. placeholder = placeholder.replace(letter2, str(eval(equation.split('=')[0])))
  63. else:
  64. equation = equation[0][::-1]
  65. letter1 = equation[0]
  66. letter2 = equation[-1]
  67. placeholder = placeholder.replace(letter1, '9')
  68. equation = equation.replace(letter1, '9')
  69. placeholder = placeholder.replace(letter2, str(9 - int(equation.split('=')[1].split('+')[0])))
  70. print("The answer of part1 is:", placeholder)
  71. return placeholder
  72. def part2(equations):
  73. placeholder = 'abcdefghijklmn'
  74. for equation in equations:
  75. if re.findall(r"\-", equation[0]):
  76. equation = equation[0][::-1]
  77. letter1 = equation[0]
  78. letter2 = equation[-1]
  79. placeholder = placeholder.replace(letter1, '1')
  80. equation = equation.replace(letter1, '1')
  81. placeholder = placeholder.replace(letter2, str(1 + int(equation.split('=')[1].split('+')[0][1])))
  82. else:
  83. equation = equation[0]
  84. letter1 = equation[0]
  85. letter2 = equation[-1]
  86. placeholder = placeholder.replace(letter1, '1')
  87. equation = equation.replace(letter1, '1')
  88. placeholder = placeholder.replace(letter2, str(eval(equation.split('=')[0])))
  89. print("The answer of part2 is:", placeholder)
  90. return placeholder
  91. if __name__ == '__main__':
  92. input, example = get_input(task_dir, 24)
  93. equations = get_equations_from_input(input)
  94. part1(equations)
  95. part2(equations)
  96. generate_readme(task_dir, 24)