-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
68 lines (57 loc) · 2.03 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
def get_monad_from_input(data):
return data.split("inp w\n")[1:]
def read_file(filename):
with open(filename) as file:
data = file.read()
return get_monad_from_input(data)
def get_largest_model_number_accepted(MONAD):
z = []
max_monad = [0] * 14
for i, instructions_to_perform in enumerate(MONAD):
instructions = instructions_to_perform.split("\n")
pop = int(instructions[3][-2:]) == 26
x_add = int(instructions[4].split()[-1])
y_add = int(instructions[14].split()[-1])
if not pop:
z.append((i, y_add))
else:
j, y_add = z.pop()
difference = x_add + y_add
if difference < 0:
max_monad[i] = 9 + difference
max_monad[j] = 9
elif difference > 0:
max_monad[i] = 9
max_monad[j] = 9 - difference
else:
max_monad[i] = max_monad[j] = 9
return "".join(map(str, max_monad))
def get_smallest_model_number_accepted(MONAD):
z = []
min_monad = [0] * 14
for i, instructions_to_perform in enumerate(MONAD):
instructions = instructions_to_perform.split("\n")
pop = int(instructions[3][-2:]) == 26
x_add = int(instructions[4].split()[-1])
y_add = int(instructions[14].split()[-1])
if not pop:
z.append((i, y_add))
else:
j, y_add = z.pop()
difference = x_add + y_add
if difference < 0:
min_monad[i] = 1
min_monad[j] = 1 - difference
elif difference > 0:
min_monad[i] = 1 + difference
min_monad[j] = 1
else:
min_monad[i] = min_monad[j] = 1
return "".join(map(str, min_monad))
if __name__ == "__main__":
MONAD = read_file("input.txt")
first_solution = get_largest_model_number_accepted(MONAD)
print(first_solution)
second_solution = get_smallest_model_number_accepted(MONAD)
print(second_solution)