-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.py
152 lines (150 loc) · 5.54 KB
/
day7.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from pathlib import Path
from itertools import permutations
from collections import defaultdict
inputFile = Path("inputs/input-07-01.txt")
splitFile = inputFile.read_text().split(",")
'''
# Part 1
phases = permutations(range(0,5))
outputs = []
for phase in phases:
inputNum = 0
for inputPhase in phase:
data = [int(i) for i in splitFile]
input2 = False
num = 0
while num < len(data):
# allows for easy parsing
inst = str(data[num])
# make sure all instrctions are of length 4
if len(inst) < 4:
inst = '0'*(4-len(inst)) + inst
# these are for opcodes that have 2 or less params
if inst[-2:] == '03':
if not input2:
data[data[num+1]] = inputPhase
input2 = True
else:
data[data[num+1]] = inputNum
num += 2
continue
# opcode 04 is a bit different in that it cares what value its outputting
elif inst[-2:] == '04':
if inst[-3] == '0':
inputNum = data[data[num+1]]
#print(data[data[num+1]])
else:
inputNum = data[num+1]
#print(data[num+1])
num += 2
continue
elif inst[-2:] == '99':
break
# this part for instructions with more than 2 params
# params determined here if they are positional or immediate
par1 = data[data[num+1]] if inst[-3] == '0' else data[num+1]
par2 = data[data[num+2]] if inst[-4] == '0' else data[num+2]
if inst[-2:] == '01':
data[data[num+3]] = par1 + par2
elif inst[-2:] == '02':
data[data[num+3]] = par1 * par2
elif inst[-2:] == '05':
if par1 != 0:
num = par2
continue
num += 3
continue
elif inst[-2:] == '06':
if par1 == 0:
num = par2
continue
num +=3
continue
elif inst[-2:] == '07':
if par1 < par2:
data[data[num+3]] = 1
else:
data[data[num+3]] = 0
elif inst[-2:] == '08':
if par1 == par2:
data[data[num+3]] = 1
else:
data[data[num+3]] = 0
num += 4
outputs.append(inputNum)
print(max(outputs))
'''
# Part 2
phases = permutations(range(5,10))
outputs = []
for phase in phases:
data = {p:[int(i) for i in splitFile] for p in phase}
inputNum = 0
feedback = False
halts = 0
nums = defaultdict(list)
while halts < 5:
for inputPhase in phase:
#data = [int(i) for i in splitFile]
input2 = False
if len(nums[inputPhase]) > 0:
num = nums[inputPhase][-1]
else:
num = 0
while num < len(data[inputPhase]):
inst = str(data[inputPhase][num])
if len(inst) < 4:
inst = '0'*(4-len(inst)) + inst
if inst[-2:] == '03':
if not input2 and not feedback:
data[inputPhase][data[inputPhase][num+1]] = inputPhase
input2 = True
else:
data[inputPhase][data[inputPhase][num+1]] = inputNum
num += 2
continue
elif inst[-2:] == '04':
if inst[-3] == '0':
inputNum = data[inputPhase][data[inputPhase][num+1]]
#print(data[data[num+1]])
else:
inputNum = data[inputPhase][num+1]
#print(data[num+1])
num += 2
nums[inputPhase].append(num)
break
elif inst[-2:] == '99':
halts += 1
break
par1 = data[inputPhase][data[inputPhase][num+1]] if inst[-3] == '0' else data[inputPhase][num+1]
par2 = data[inputPhase][data[inputPhase][num+2]] if inst[-4] == '0' else data[inputPhase][num+2]
if inst[-2:] == '01':
data[inputPhase][data[inputPhase][num+3]] = par1 + par2
elif inst[-2:] == '02':
data[inputPhase][data[inputPhase][num+3]] = par1 * par2
elif inst[-2:] == '05':
if par1 != 0:
num = par2
continue
num += 3
continue
elif inst[-2:] == '06':
if par1 == 0:
num = par2
continue
num +=3
continue
elif inst[-2:] == '07':
if par1 < par2:
data[inputPhase][data[inputPhase][num+3]] = 1
else:
data[inputPhase][data[inputPhase][num+3]] = 0
elif inst[-2:] == '08':
if par1 == par2:
data[inputPhase][data[inputPhase][num+3]] = 1
else:
data[inputPhase][data[inputPhase][num+3]] = 0
num += 4
feedback = True
outputs.append(inputNum)
print(max(outputs))