I really enjoyed this one, actually. This is so much better than this time last year, when I was doing all kinds of input parsing tricks and tripping over log files. Give me straight-up integers in a comma-separated list any day!
I decided to make my code really readable and branched out into creating 2 Python enums and a class. It made writing the code feel like a snap, as I no longer had to wrap my brain around all kinds of numbers and could hang my head on descriptive operations.
A couple of the lines are unnecessary (like writing the repl function as well as there's a DRY violation with parameter interpretation)...but I really like the ternary if/then/else operator in Python. I'm used to Java's print(x == 5 ? "yup" : "nope")... print("yup" if x is 5 else "nope") is far superior!
fromenumimportEnum# Returns the number at the given position (0 being the rightmost)
defget_nth_digit(n,number):returnnumber//10**n%10classOperation(Enum):ADDITION=1MULTIPLICATION=2INPUT=3OUTPUT=4JUMP_IF_TRUE=5JUMP_IF_FALSE=6LESS_THAN=7EQUALS=8TERMINATION=99classMode(Enum):POSITION=0IMMEDIATE=1classInstruction:def__init__(self,opcode):# instruction: 1 is add, 2 is multiply, 3 is input, 4 is output, 99 is end
self.operation=Operation(get_nth_digit(1,opcode)*10+get_nth_digit(0,opcode))# mode: 0 is indirect, 1 is immediate
self.modes=list(map(Mode,[get_nth_digit(2,opcode),get_nth_digit(3,opcode),get_nth_digit(4,opcode)]))def__str__(self):return"{}, {}".format(repr(self.operation),self.modes)defopcode_run(ops):i=0whileops[i]!=99:instruction=Instruction(ops[i])ifinstruction.operationisOperation.ADDITION:first=ops[i+1]ifinstruction.modes[0]isMode.IMMEDIATEelseops[ops[i+1]]second=ops[i+2]ifinstruction.modes[1]isMode.IMMEDIATEelseops[ops[i+2]]result=first+second# the last mode should *always* be POSITION
ops[ops[i+3]]=resulti+=4elifinstruction.operationisOperation.MULTIPLICATION:first=ops[i+1]ifinstruction.modes[0]isMode.IMMEDIATEelseops[ops[i+1]]second=ops[i+2]ifinstruction.modes[1]isMode.IMMEDIATEelseops[ops[i+2]]val=first*second# the last mode should *always* be POSITION
ops[ops[i+3]]=vali+=4elifinstruction.operationisOperation.INPUT:stuff=input("Please enter ID: --> ")ops[ops[i+1]]=int(stuff)i+=2elifinstruction.operationisOperation.OUTPUT:print(ops[ops[i+1]])i+=2elifinstruction.operationisOperation.JUMP_IF_TRUE:first=ops[i+1]ifinstruction.modes[0]isMode.IMMEDIATEelseops[ops[i+1]]second=ops[i+2]ifinstruction.modes[1]isMode.IMMEDIATEelseops[ops[i+2]]iffirst!=0:i=secondelse:i+=3elifinstruction.operationisOperation.JUMP_IF_FALSE:first=ops[i+1]ifinstruction.modes[0]isMode.IMMEDIATEelseops[ops[i+1]]second=ops[i+2]ifinstruction.modes[1]isMode.IMMEDIATEelseops[ops[i+2]]iffirst==0:i=secondelse:i+=3elifinstruction.operationisOperation.LESS_THAN:first=ops[i+1]ifinstruction.modes[0]isMode.IMMEDIATEelseops[ops[i+1]]second=ops[i+2]ifinstruction.modes[1]isMode.IMMEDIATEelseops[ops[i+2]]ops[ops[i+3]]=1iffirst<secondelse0i+=4elifinstruction.operationisOperation.EQUALS:first=ops[i+1]ifinstruction.modes[0]isMode.IMMEDIATEelseops[ops[i+1]]second=ops[i+2]ifinstruction.modes[1]isMode.IMMEDIATEelseops[ops[i+2]]ops[ops[i+3]]=1iffirst==secondelse0i+=4print("HALT!")# Initialize: open file, turn all op codes into integers
withopen('day05.txt')asf:# split line into operation list
opsAsStrings=f.read().split(",")# turn them all into integers
ops=list(map(int,opsAsStrings))# Part One: Enter 1 when prompted and enter number right before HALT!
# Part Two: Enter 5 when prompted and enter number right before HALT!
myOps=ops.copy()opcode_run(myOps)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I really enjoyed this one, actually. This is so much better than this time last year, when I was doing all kinds of input parsing tricks and tripping over log files. Give me straight-up integers in a comma-separated list any day!
I decided to make my code really readable and branched out into creating 2 Python enums and a class. It made writing the code feel like a snap, as I no longer had to wrap my brain around all kinds of numbers and could hang my head on descriptive operations.
A couple of the lines are unnecessary (like writing the repl function as well as there's a DRY violation with parameter interpretation)...but I really like the ternary if/then/else operator in Python. I'm used to Java's
print(x == 5 ? "yup" : "nope")
...print("yup" if x is 5 else "nope")
is far superior!