DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 9: Sensor Boost

Collapse
 
jyothi9999 profile image
jyothi9999

"""
Hi,
now i am learning python
i also wrote a program in very basic
but i did not get the answer
can u give me any hint
"""

import numpy as np

f = open("input9","r") # file opening
code = f.read() + ',0' * 1000 # for reading files with commas(,) as list
nList = code.split(',')
f.close() # file closing

print(nList)

for i in range(0, len(nList)): # converting elements of List(strings) into integers
nList[i] = int(nList[i])

print(len(nList))

def get_digit(number, n):
return number // 10**n % 10

def intcode(nList,in1):
List = nList.copy()
for i,each in enumerate(nList):
List[i] = int(each)
i = 0
rb = 0

while i < len(nList):
    n = List[i]
    op = 10 * get_digit(n, 1) + get_digit(n, 0)
    p1_mode = get_digit(n, 2)
    p2_mode = get_digit(n, 3)
    p3_mode = get_digit(n, 1)

    if op == 1 or op == 2 or op == 7 or op == 8:
        int1 = int2 = position = p1 = p2 = 0
        int1 = List[i + 1]
        int2 = List[i + 2]
        int3 = List[i + 3]

        if p1_mode == 1:
            p1 = int1
        elif p1_mode == 0 and int1 >= 0 :
            p1 = List[int1]
        elif p1_mode == 2 and int1 + rb >= 0:
            p1 = List[int1 + rb]
        else:
            print(f'ERROR - invalid parameter mode: {p1_mode}')

        if p2_mode == 1:
            p2 = int2
        elif p2_mode == 0 and int1 >= 0:
            p2 = List[int2]
        elif p2_mode == 2 and int2 + rb >= 0:
            p2 = List[int2 + rb]
        else:
            print(f'ERROR - invalid parameter mode: {p2_mode}')

        if p3_mode == 0 and int1 >= 0:           # p3_mode noi in immediate mode (!= 1)
            position = List[int3]
        elif p3_mode == 2 and int3 + rb >= 0:
            position = List[int3 + rb]
        else:
            print(f'ERROR - invalid parameter mode: {p3_mode}')

        if op == 1:
            add = p1 + p2
            List[position] = add
        elif op == 2:
            multi = p1 * p2
            List[position] = multi
        elif op == 7:
            if p1 < p2:
                List[position] = 1
            else:
                List[position] = 0
        elif op == 8:
            if p1 == p2:
                List[position] = 1
            else:
                List[position] = 0

        i = i + 4
        continue

    elif op == 3 or op == 4 or op == 9:

        int1 = p1 = 0
        int1 = List[i + 1]

        if p1_mode == 1:
            print('ERROR - immediate mode while writing to memory')
        elif p1_mode == 0 and int1 >= 0:
            print(int1)
            p1 = List[int1]
        elif p1_mode == 2 and int1 + rb >= 0:
            print(int1 + rb)
            p1 = List[int1 + rb]
        else:
            print(f'ERROR - invalid parameter mode: {p1_mode}')

        if op == 3:
            if p1_mode == 1:
                List[i+1] = in1
            elif p1_mode == 0 and List[i+1] >= 0:
                List[List[i+1]] = in1
            elif p1_mode == 2 and List[i+1] + rb >= 0:
                List[List[i+1] + rb] = in1
            else:
                print(f'ERROR - invalid parameter mode: {p1_mode}')

        elif op == 4:
            output = p1
            print('output ----------------------------------> ', output)

        elif op == 9:
            rb = rb + p1
            print("rb........................", rb)

        i = i + 2
        continue

    elif op == 5 or op == 6:

        int1 = int2 = p1 = p2 = 0
        int1 = List[i + 1]
        int2 = List[i + 2]

        if p1_mode == 1:
            p1 = int1
        elif p1_mode == 0 and int1 >= 0:
            p1 = List[int1]
        elif p1_mode == 2 and int1 + rb >= 0:
            p1 = List[int1 + rb]
        else:
            print(f'ERROR - invalid parameter mode: {p1_mode}')

        if p2_mode == 1:
            p2 = int2
        elif p2_mode == 0 and int1 >= 0:
            p2 = List[int2]
        elif p2_mode == 2 and int2 + rb >= 0:
            p2 = List[int2 + rb]
        else:
            print(f'ERROR - invalid parameter mode: {p2_mode}')

        if (op == 5 and p1 != 0) or (op == 6 and p1 == 0):
            i = p2
            continue
        i = i + 3
        continue

    elif op == 99:
        print("********* PROGRAM HALTED *********")
        break

    else:
        print(f'ERROR - invalid opcode {op}')
        break

return List

boost_program = intcode(nList,1)
print(boost_program)

print(intcode(boost_program,1))[1]