Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. input = open("InputDay5.txt", "r") #open file
  2. puzzle = list(map(int, input.read().split(','))) #make a 'puzzle' list splitted by colon
  3.  
  4. inputValue = 1
  5. def mode(type, list, index): #return value depending on immediate or position mode
  6.     if type == 1:
  7.         return list[index]
  8.     else:
  9.         return list[list[index]]
  10.  
  11. index = 0
  12. while puzzle[index] != 99:
  13.     instruction = [int(x) for x in str(puzzle[index])] #make a list of instruction digits, fill to len(5) with zeros if needed
  14.     while len(instruction) < 5:
  15.         instruction.insert(0,0)
  16.     if instruction[-1] == 1:
  17.         #opcode 1: add values from next two indexes/addresses, save it under address from 3rd
  18.         puzzle[puzzle[index+3]] = mode(instruction[-3], puzzle, index+1) + mode(instruction[-4], puzzle, index+2)
  19.         index+=4
  20.     elif instruction[-1] == 2:
  21.         #opcode 2: multiply values from next two indexes/addresses, save it under address from 3rd
  22.         puzzle[puzzle[index+3]] = mode(instruction[-3], puzzle, index+1) * mode(instruction[-4], puzzle, index+2)
  23.         index+=4
  24.     elif instruction[-1] == 3:
  25.         #opcode 3: take input value and save it under address from next index
  26.         puzzle[puzzle[index+1]]=inputValue
  27.         index+=2
  28.     elif instruction[-1] == 4:
  29.         #opcode 4: output value from address from next index
  30.         print(puzzle[puzzle[index+1]])
  31.         index+=2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement