Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd1bc9e6

Browse files
committed
IntCode class updated (Day 7-compatible)
1 parent0f35a02 commitd1bc9e6

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

‎2019/IntCode.py‎

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
classIntCode:
2-
instructions= []
3-
pointer=0
4-
state="Running"
5-
modes="000"
6-
inputs= []
7-
outputs= []
2+
# Verbosity
83
verbose_level=0
4+
5+
# Count of parameters per opcode
96
instr_length= {
107
"01":4,
118
"02":4,
@@ -18,14 +15,38 @@ class IntCode:
1815
"99":1,
1916
}
2017

21-
def__init__(self,instructions):
18+
def__init__(self,instructions,reference=""):
2219
self.instructions=list(map(int,instructions.split(",")))
20+
self.reference=reference
21+
22+
# Current state
23+
self.pointer=0
24+
self.state="Running"
25+
26+
# Current instruction modes
27+
self.modes="000"
28+
29+
# Inputs and outputs
30+
self.inputs= []
31+
self.all_inputs= []
32+
self.outputs= []
2333

2434
defreset(self,instructions):
2535
self.instructions=list(map(int,instructions.split(",")))
2636
self.pointer=0
2737
self.state="Running"
2838

39+
defrestart(self):
40+
self.state="Running"
41+
42+
defadd_input(self,value):
43+
try:
44+
self.inputs+=value
45+
self.all_inputs+=value
46+
except:
47+
self.inputs.append(value)
48+
self.all_inputs.append(value)
49+
2950
defget_opcode(self):
3051
instr=self.instructions[self.pointer]
3152
opcode_full="0"* (5-len(str(instr)))+str(instr)
@@ -53,6 +74,9 @@ def op_02(self, instr):
5374
self.state="Running"
5475

5576
defop_03(self,instr):
77+
iflen(self.inputs)==0:
78+
self.state="Paused"
79+
return
5680
self.instructions[instr[1]]=self.inputs.pop(0)
5781
self.pointer+=self.instr_length["03"]
5882
self.state="Running"
@@ -111,9 +135,18 @@ def run(self):
111135
print("Instructions:",",".join(map(str,self.instructions)))
112136

113137
defexport(self):
114-
instr=",".join(map(str,self.instructions))
115-
inputs=",".join(map(str,self.inputs))
116-
outputs=",".join(map(str,self.outputs))
117-
return (
118-
"Instructions: "+instr+"\nInputs: "+inputs+"\nOutputs: "+outputs
119-
)
138+
output=""
139+
ifself.reference!="":
140+
output+="Computer # "+str(self.reference)
141+
output+="\n"+"Instructions: "+",".join(map(str,self.instructions))
142+
output+="\n"+"Inputs: "+",".join(map(str,self.all_inputs))
143+
output+="\n"+"Outputs: "+",".join(map(str,self.outputs))
144+
returnoutput
145+
146+
defexport_io(self):
147+
output=""
148+
ifself.reference!="":
149+
output+="Computer # "+str(self.reference)
150+
output+="\n"+"Inputs: "+",".join(map(str,self.all_inputs))
151+
output+="\n"+"Outputs: "+",".join(map(str,self.outputs))
152+
returnoutput

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp