-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_7_part1.py
executable file
·64 lines (63 loc) · 1.7 KB
/
day_7_part1.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
import itertools
with open("input_7.txt") as f:
nums = [int(i) for i in f.read().split(",")]
all_seq = itertools.permutations([0, 1, 2, 3, 4])
result = 0
for current_seq in all_seq:
stdin = 0
stdin_b = False
for phase in current_seq:
i = 0
temp = nums[:]
while i < len(temp):
num = str(temp[i]).zfill(5)
opcode = int(num[3:])
mode_1 = int(num[2])
mode_2 = int(num[1])
if opcode == 1:
temp[temp[i+3]] = (temp[i+1] if mode_1 else temp[temp[i+1]]) + (temp[i+2] if mode_2 else temp[temp[i+2]])
i += 4
elif opcode == 2:
temp[temp[i+3]] = (temp[i+1] if mode_1 else temp[temp[i+1]]) * (temp[i+2] if mode_2 else temp[temp[i+2]])
i += 4
elif opcode == 3:
if stdin_b:
temp[temp[i+1]] = stdin
stdin_b = False
else:
temp[temp[i+1]] = phase
stdin_b = True
i += 2
elif opcode == 4:
stdin = temp[i+1] if mode_1 else temp[temp[i+1]]
i += 2
elif opcode == 5:
if (temp[i+1] if mode_1 else temp[temp[i+1]]):
i = temp[i+2] if mode_2 else temp[temp[i+2]]
else:
i += 3
elif opcode == 6:
if not (temp[i+1] if mode_1 else temp[temp[i+1]]):
i = temp[i+2] if mode_2 else temp[temp[i+2]]
else:
i += 3
elif opcode == 7:
if (temp[i+1] if mode_1 else temp[temp[i+1]]) < (temp[i+2] if mode_2 else temp[temp[i+2]]):
temp[temp[i+3]] = 1
else:
temp[temp[i+3]] = 0
i += 4
elif opcode == 8:
if (temp[i+1] if mode_1 else temp[temp[i+1]]) == (temp[i+2] if mode_2 else temp[temp[i+2]]):
temp[temp[i+3]] = 1
else:
temp[temp[i+3]] = 0
i += 4
elif opcode == 99:
break
else:
print(f"Unknown: Position: {i} Value: {temp[i]}")
break
if stdin > result:
result = stdin
print(result)