mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-12-22 05:41:51 +01:00
Solve day 5 part 1
This commit is contained in:
parent
070296abe3
commit
efb48beccc
2 changed files with 62 additions and 0 deletions
61
2019/day05-intcode.py
Normal file
61
2019/day05-intcode.py
Normal file
|
@ -0,0 +1,61 @@
|
|||
ZERO_PARAM_CODES = [99]
|
||||
ONE_PARAM_CODES = [3, 4]
|
||||
THREE_PARAMS_CODES = [1, 2]
|
||||
|
||||
|
||||
def compute(lst):
|
||||
result = lst.copy()
|
||||
|
||||
i = 0
|
||||
while i < len(result):
|
||||
first_instruction = str(result[i])
|
||||
code = int(first_instruction[-2:])
|
||||
param_modes = first_instruction[:-2]
|
||||
if code == 99:
|
||||
print("Halting due to code 99")
|
||||
return result
|
||||
if code in THREE_PARAMS_CODES:
|
||||
offset = 4
|
||||
param_modes = param_modes.zfill(offset - 1)
|
||||
param_modes_parsed = list(map(int, reversed(param_modes)))
|
||||
|
||||
position1, position2 = result[i + 1], result[i + 2]
|
||||
if param_modes_parsed[0] == 0:
|
||||
item1 = result[position1]
|
||||
else:
|
||||
item1 = position1
|
||||
if param_modes_parsed[1] == 0:
|
||||
item2 = result[position2]
|
||||
else:
|
||||
item2 = position2
|
||||
result_index = result[i + 3]
|
||||
|
||||
if code == 1:
|
||||
result[result_index] = item1 + item2
|
||||
elif code == 2:
|
||||
result[result_index] = item1 * item2
|
||||
|
||||
elif code in ONE_PARAM_CODES:
|
||||
offset = 2
|
||||
param_mode = int(param_modes.zfill(offset - 1))
|
||||
position = result[i + 1]
|
||||
if code == 3:
|
||||
result[position] = int(input(f"Input for instruction {i}"))
|
||||
elif code == 4:
|
||||
if param_mode == 0:
|
||||
res = result[position]
|
||||
else:
|
||||
res = position
|
||||
print(res)
|
||||
|
||||
else:
|
||||
raise ValueError(f"Something bad happened, code={code}")
|
||||
|
||||
i += offset
|
||||
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("inputs/day05") as f:
|
||||
compute(list(map(int, f.read().split(","))))
|
1
2019/inputs/day05
Normal file
1
2019/inputs/day05
Normal file
|
@ -0,0 +1 @@
|
|||
3,225,1,225,6,6,1100,1,238,225,104,0,1001,92,74,224,1001,224,-85,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,1101,14,63,225,102,19,83,224,101,-760,224,224,4,224,102,8,223,223,101,2,224,224,1,224,223,223,1101,21,23,224,1001,224,-44,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,1102,40,16,225,1102,6,15,225,1101,84,11,225,1102,22,25,225,2,35,96,224,1001,224,-350,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,1101,56,43,225,101,11,192,224,1001,224,-37,224,4,224,102,8,223,223,1001,224,4,224,1,223,224,223,1002,122,61,224,1001,224,-2623,224,4,224,1002,223,8,223,101,7,224,224,1,223,224,223,1,195,87,224,1001,224,-12,224,4,224,1002,223,8,223,101,5,224,224,1,223,224,223,1101,75,26,225,1101,6,20,225,1102,26,60,224,101,-1560,224,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,108,677,226,224,102,2,223,223,1006,224,329,1001,223,1,223,1108,226,677,224,1002,223,2,223,1006,224,344,101,1,223,223,7,226,677,224,102,2,223,223,1006,224,359,1001,223,1,223,1007,226,677,224,1002,223,2,223,1006,224,374,1001,223,1,223,1108,677,226,224,102,2,223,223,1005,224,389,1001,223,1,223,107,226,226,224,102,2,223,223,1006,224,404,101,1,223,223,1107,226,226,224,1002,223,2,223,1005,224,419,1001,223,1,223,1007,677,677,224,102,2,223,223,1006,224,434,101,1,223,223,1107,226,677,224,1002,223,2,223,1006,224,449,101,1,223,223,107,677,677,224,102,2,223,223,1005,224,464,1001,223,1,223,1008,226,226,224,1002,223,2,223,1005,224,479,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,494,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,509,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,524,1001,223,1,223,1008,677,677,224,102,2,223,223,1006,224,539,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,554,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,569,101,1,223,223,107,677,226,224,102,2,223,223,1005,224,584,101,1,223,223,8,226,226,224,1002,223,2,223,1005,224,599,101,1,223,223,108,226,226,224,1002,223,2,223,1006,224,614,1001,223,1,223,7,226,226,224,102,2,223,223,1006,224,629,1001,223,1,223,1107,677,226,224,102,2,223,223,1005,224,644,101,1,223,223,8,226,677,224,102,2,223,223,1006,224,659,1001,223,1,223,1008,226,677,224,1002,223,2,223,1006,224,674,1001,223,1,223,4,223,99,226
|
Loading…
Reference in a new issue