/ Python / 2021 / 24.py
24.py
 1  from lib import *
 2  
 3  input = read_input(2021, 24)
 4  
 5  lines = input.splitlines()
 6  
 7  
 8  stack = []
 9  out = [9] * 14
10  for i in range(14):
11      x = int(lines[i * 18 + 5].split()[2])
12      y = int(lines[i * 18 + 15].split()[2])
13      if x < 0:
14          j, y = stack.pop()
15          x += y
16          if x < 0:
17              out[i] += x
18          else:
19              out[j] -= x
20      else:
21          stack.append((i, y))
22  
23  print(int("".join(map(str, out))))
24  
25  
26  stack = []
27  out = [1] * 14
28  for i in range(14):
29      x = int(lines[i * 18 + 5].split()[2])
30      y = int(lines[i * 18 + 15].split()[2])
31      if x < 0:
32          j, y = stack.pop()
33          x += y
34          if x < 0:
35              out[j] -= x
36          else:
37              out[i] += x
38      else:
39          stack.append((i, y))
40  
41  print(int("".join(map(str, out))))