Add solution to day2 part 1
parent
0041ab0045
commit
b5d2571284
|
@ -0,0 +1 @@
|
|||
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,6,19,23,1,10,23,27,2,27,13,31,1,31,6,35,2,6,35,39,1,39,5,43,1,6,43,47,2,6,47,51,1,51,5,55,2,55,9,59,1,6,59,63,1,9,63,67,1,67,10,71,2,9,71,75,1,6,75,79,1,5,79,83,2,83,10,87,1,87,5,91,1,91,9,95,1,6,95,99,2,99,10,103,1,103,5,107,2,107,6,111,1,111,5,115,1,9,115,119,2,119,10,123,1,6,123,127,2,13,127,131,1,131,6,135,1,135,10,139,1,13,139,143,1,143,13,147,1,5,147,151,1,151,2,155,1,155,5,0,99,2,0,14,0
|
|
@ -0,0 +1,31 @@
|
|||
import itertools
|
||||
|
||||
|
||||
def part1(inputs):
|
||||
inputs[1] = 12
|
||||
inputs[2] = 2
|
||||
for i, opcode in itertools.islice(enumerate(inputs), 0, None, 4):
|
||||
operation = None
|
||||
# Terminate opcode
|
||||
if opcode == 99:
|
||||
break
|
||||
elif opcode == 1:
|
||||
operation = lambda a, b: a + b
|
||||
elif opcode == 2:
|
||||
operation = lambda a, b: a * b
|
||||
else:
|
||||
raise Exception('Bad opcode: ' + str(opcode))
|
||||
|
||||
input_a_index = inputs[i + 1]
|
||||
input_b_index = inputs[i + 2]
|
||||
out_index = inputs[i + 3]
|
||||
inputs[out_index] = operation(inputs[input_a_index], inputs[input_b_index])
|
||||
|
||||
return inputs[0]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
with open('input.txt') as f:
|
||||
inputs = [int(item) for item in f.read().rstrip().split(',')]
|
||||
|
||||
print(part1(inputs))
|
Loading…
Reference in New Issue