-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD13-1.py
125 lines (116 loc) · 4.52 KB
/
D13-1.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
def memory(Intcode):
upgradeAC=Intcode
instruction_pointer=0
relative_base=0
output_pool = []
while True:
instruction=upgradeAC[instruction_pointer]
parameters1=int(upgradeAC[instruction_pointer+1])
parameters2=int(upgradeAC[instruction_pointer+2])
parameters3=int(upgradeAC[instruction_pointer+3])
modeH={'0':int(upgradeAC[parameters1]),'1':parameters1,'2':int(upgradeAC[parameters1+relative_base])}
modeT={'0':int(upgradeAC[parameters2]),'1':parameters2,'2':int(upgradeAC[parameters2+relative_base])}
forH=modeH[str(instruction)[-3]]
forT=modeT[str(instruction)[-4]]
if str(instruction)[3:]=='01':
add4=forH+forT
if str(instruction)[0]=='0':
upgradeAC[parameters3]=int(add4)
elif str(instruction)[0]=='2':
upgradeAC[parameters3+relative_base]=int(add4)
instruction_pointer+=4
elif str(instruction)[3:]=='02':
multiply4=forH*forT
if str(instruction)[0]=='0':
upgradeAC[parameters3]=int(multiply4)
elif str(instruction)[0]=='2':
upgradeAC[parameters3+relative_base]=int(multiply4)
instruction_pointer+=4
elif str(instruction)[3:]=='03':
ACuser=input("BOOST program asks for a single input:")
if str(instruction)[-3]=='0':
print('Pointer0',instruction_pointer)
upgradeAC[parameters1]=int(ACuser)
elif str(instruction)[-3]=='2':
upgradeAC[parameters1+relative_base]=int(ACuser)
instruction_pointer+=2
elif str(instruction)[3:]=='04':
# print('opcode 4H0',forH)
output_pool.append(forH)
instruction_pointer+=2
elif str(instruction)[3:]=='05':
if forH is 0:
instruction_pointer+=3
else:
instruction_pointer=forT
if instruction_pointer>len(Intcode):
print('something too big1')
break
elif str(instruction)[3:]=='06':
if forH is 0:
instruction_pointer=forT
if instruction_pointer>len(Intcode):
print('something too big5')
break
else:
instruction_pointer+=3
elif str(instruction)[3:]=='07':
if forH<forT:
if str(instruction)[0]=='0':
upgradeAC[parameters3]=1
elif str(instruction)[0]=='2':
upgradeAC[parameters3+relative_base]=1
else:
if str(instruction)[0]=='0':
upgradeAC[parameters3]=0
elif str(instruction)[0]=='2':
upgradeAC[parameters3+relative_base]=0
instruction_pointer+=4
elif str(instruction)[3:]=='08':
if forH==forT:
if str(instruction)[0]=='0':
upgradeAC[parameters3]=1
elif str(instruction)[0]=='2':
upgradeAC[parameters3+relative_base]=1
else:
if str(instruction)[0]=='0':
upgradeAC[parameters3]=0
elif str(instruction)[0]=='2':
upgradeAC[parameters3+relative_base]=0
instruction_pointer+=4
elif str(instruction)[3:]=='09':
relative_base=forH+relative_base
instruction_pointer+=2
elif str(instruction)[3:]=='99':
print('Stop for 99 at',instruction_pointer,'\n')
break
else:
print('Something went wrong.')
break
print('The lengh of output:',len(output_pool))
print('Output:'+'\n', output_pool)
block = 0
for tile in range(0,len(output_pool),3):
if output_pool[tile+2] == 2:
block+=1
# elif output_pool[tile+2] == 4:
# print('this is ball')
print('Total block number is ',block)
with open('D13-data.txt','r') as intcode:
int_data=intcode.read()
arcade_input=list(int_data.split(','))
numerize=[]
for item in arcade_input:
numerize.append(int(item))
# print(max(numerize),len(numerize))
for position in range(len(numerize),(max(numerize)+100)):
numerize.insert(position,0)
# new way to add 0 and become 5-digits
arcade_input=[]
for item in numerize:
new_item='%05d' % item
arcade_input.append(new_item)
# print(arcade_input)
# print(len(arcade_input))
memory(arcade_input)
intcode.close()