Solve day 7 part 1

This commit is contained in:
Gabriel Augendre 2020-03-05 22:16:19 +01:00
parent f633e19cea
commit ba3b3b90ee
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
5 changed files with 138 additions and 0 deletions

134
2019/day07-intcode.py Normal file
View file

@ -0,0 +1,134 @@
import itertools
from typing import List
NUMBER_OF_PARAMS_MAP = {
1: 3,
2: 3,
3: 1,
4: 1,
5: 2,
6: 2,
7: 3,
8: 3,
}
LAST_IS_RESULT_MAP = {
1: True,
2: True,
3: True,
4: False,
5: False,
6: False,
7: True,
8: True,
}
def get_value(program, args, param_modes, index):
if param_modes[index] == 0:
return program[args[index]]
return args[index]
def parse_args(program, raw_args, param_modes, last_is_result=False):
args = []
limit = -1 if last_is_result else None
for i, arg in enumerate(raw_args[:limit]):
args.append(get_value(program, raw_args, param_modes, i))
if last_is_result:
args.append(raw_args[-1])
return args
def compute(lst: List[int], inputs: List[int] = None) -> List[int]:
if inputs is None:
inputs = []
program = lst.copy() # type: List[int]
outputs = [] # type: List[int]
pointer = 0
while pointer < len(program):
pointer_moved = False
instruction = str(program[pointer])
code = int(instruction[-2:])
if code == 99:
return outputs
number_of_params = NUMBER_OF_PARAMS_MAP[code]
offset = number_of_params + 1
param_modes = instruction[:-2]
param_modes = param_modes.zfill(number_of_params)
param_modes = list(map(int, reversed(param_modes)))
raw_params = []
for i in range(1, offset):
raw_params.append(program[pointer + i])
last_is_result = LAST_IS_RESULT_MAP[code]
params = parse_args(program, raw_params, param_modes, last_is_result)
if code == 1:
# Addition
program[params[2]] = params[0] + params[1]
elif code == 2:
# Multiplication
program[params[2]] = params[0] * params[1]
elif code == 3:
# Input
try:
input_value = int(inputs.pop(0))
except IndexError:
input_value = int(input(f"Input for instruction {pointer}\n> "))
program[params[0]] = input_value
elif code == 4:
# Output
output_value = params[0]
outputs.append(output_value)
elif code == 5:
# Jump if true
if params[0] != 0:
pointer = params[1]
pointer_moved = True
elif code == 6:
# Jump if false
if params[0] == 0:
pointer = params[1]
pointer_moved = True
elif code == 7:
# Less than
if params[0] < params[1]:
program[params[2]] = 1
else:
program[params[2]] = 0
elif code == 8:
# Equals
if params[0] == params[1]:
program[params[2]] = 1
else:
program[params[2]] = 0
else:
raise ValueError(f"Something bad happened, code={code}")
if not pointer_moved:
pointer += offset
return program
def main():
with open("inputs/day07") as input_file:
original_program = list(map(int, input_file.read().split(",")))
values = set()
for phase in itertools.permutations("01234"):
amp1 = compute(original_program, [int(phase[0]), 0])
amp2 = compute(original_program, [int(phase[1]), amp1[0]])
amp3 = compute(original_program, [int(phase[2]), amp2[0]])
amp4 = compute(original_program, [int(phase[3]), amp3[0]])
amp5 = compute(original_program, [int(phase[4]), amp4[0]])
values.add(amp5[0])
print(max(values))
if __name__ == "__main__":
main()

1
2019/inputs/day07 Normal file
View file

@ -0,0 +1 @@
3,8,1001,8,10,8,105,1,0,0,21,34,43,64,85,98,179,260,341,422,99999,3,9,1001,9,3,9,102,3,9,9,4,9,99,3,9,102,5,9,9,4,9,99,3,9,1001,9,2,9,1002,9,4,9,1001,9,3,9,1002,9,4,9,4,9,99,3,9,1001,9,3,9,102,3,9,9,101,4,9,9,102,3,9,9,4,9,99,3,9,101,2,9,9,1002,9,3,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,99

1
2019/inputs/day07-ex1 Normal file
View file

@ -0,0 +1 @@
3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0

1
2019/inputs/day07-ex2 Normal file
View file

@ -0,0 +1 @@
3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0

1
2019/inputs/day07-ex3 Normal file
View file

@ -0,0 +1 @@
3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0