Advent of Code 2019 馃く (讬讜诐 2)

砖专砖讜专 Advent of Code 讬讜诐 1 谞讞诇 讛爪诇讞讛 讙讚讜诇讛.
讛讜讗 讛讬讛 讘讚讬拽转 讟诪驻专讟讜专讛 讟讜讘讛 诇诪讬诐, 讗讘诇 注讻砖讬讜 讛讙讬注 讛讝诪谉 诇讛注诇讜转 讗转 讛讗谞讟讛 讜诇驻转讜专 讗转 讬讜诐 2.

讗谞讬 诪谞讬讞 砖讛讗转讙专 讛讝讛 讬砖讗专 驻讛 诇驻讞讜转 诇讬讜诪讬讬诐 砖诇讜砖讛, 讗讝 拽讞讜 讗转 讛讝诪谉 :slight_smile:
讜讗诐 诇讗 讛住驻拽转诐 诇驻转讜专 讗转 讬讜诐 讗讞讚 鈥 诇讗 讞讜讘讛, 讗讘诇 诇讻讜 注诇 讝讛! 讝讛 转专讙讬诇 谞讞诪讚 诪讗讜讚.

讗讝 拽讚讬诪讛, 驻专住诪讜 驻讛 讗转 讛驻转专讜谞讜转 砖诇讻诐 诇讬讜诐 讛砖谞讬 砖诇 Advent of Code!

3 诇讬讬拽讬诐

讗谞讬 诪砖转讙注 诪讛讛讜专讗讜转 讛讗诇讜 , 讝讛 讛诪讙讬诇讜转 讛讙谞讜讝讜转 鈥 讛专讘讛 讬讜转专 拽砖讛 诇讬 诇讛讘讬谉 讗转 讛讗谞讙诇讬转 讜诪讛 砖讛诐 专讜爪讬诐 诪讗砖专 诇讻转讜讘 讗转 讛拽讜讚.
讝讛 讛讞诇拽 讛专讗砖讜谉 讘讻诇 讗讜驻谉 鈥

诇讬讬拽 1

讗谞讬 诪住讻讬诐, 诇诪注谉 讛讗诪转.
讝讛 讙诐 诪砖讛讜 砖讗谞讞谞讜 诪拽驻讬讚讬诐 注诇讬讜 讘讻转讬讘转 转专讙讬诇讬诐 讗爪诇谞讜 鈥 讗诐 讗讬谉 爪讜专讱, 讛诪诇诇 拽爪专. 讘诇讬 住讬驻讜专讬 专拽注.
讝讛 谞讞诪讚 诪爪讚 讗讞讚 砖讬砖 住讬驻讜专 讘讛诪砖讻讬诐. 诪爪讚 砖谞讬 诇驻注谞讞 诪讛 讛讘谞讗讚诐 专讜爪讛 讬讻讜诇 诇拽讞转 讛专讘讛 诪讚讬 讝诪谉

2 诇讬讬拽讬诐

讛讬转讛 转拽讜驻讛 砖讛讬讬转讬 注讜砖讛 challenge 讬讜诪讬 诪讛讗转专 讛讝讛 -
诪讗讜讚 讗讜讛讘 讗转 讛驻诇讟驻讜专诪讛 砖诇讛诐.

拽讬讘诇转讬 转砖讜讘讛 诇住注讬祝 讘鈥 - 砖砖谞讬 讛诪住驻专讬诐 砖讬讜爪专讬诐 19690720 讛诐 37 讜 49
讗讱 诇爪注专讬 讛诐 讟讜注谞讬诐 砖讛讬讗 诇讗 谞讻讜谞讛 :frowning:
转讬拽讜谉 : 讛讬讗 讻谉 谞讻讜谞讛 , 讟注讜转 讟讬驻砖讬转 鈥

讛拽讜讚 砖诇讬 讻讗谉 - 讗诐 诪讬砖讛讜 诪注讜谞讬讬谉 鈥

讞诇拽 1

讞诇拽 2

转诪讜谞讛

3 诇讬讬拽讬诐
转砖讜讘讛
with open('resources/input_day2.txt', 'r') as fh:
    opcodes = fh.readlines()


def get_list_of_opcodes():
    return opcodes[0].strip().split(',')


def add_action(list_of_opcodes, item1_position, item2_position, placement_position):
    list_of_opcodes[placement_position] = int(list_of_opcodes[item1_position]) + int(list_of_opcodes[item2_position])
    return list_of_opcodes


def multiple_action(list_of_opcodes, item1_position, item2_position, placement_position):
    list_of_opcodes[placement_position] = int(list_of_opcodes[item1_position]) * int(list_of_opcodes[item2_position])
    return list_of_opcodes


def gravity_assist(tpl):
    list_of_opcodes = get_list_of_opcodes()
    list_of_opcodes[1] = tpl[0]
    list_of_opcodes[2] = tpl[1]
    index = 0
    while index < len(list_of_opcodes) and int(list_of_opcodes[index]) != 99:
        code = int(list_of_opcodes[index])
        item1_position = int(list_of_opcodes[index + 1])
        item2_position = int(list_of_opcodes[index + 2])
        placement_position = int(list_of_opcodes[index + 3])
        if code == 1:
            add_action(list_of_opcodes, item1_position, item2_position, placement_position)
            index += 4
        elif code == 2:
            multiple_action(list_of_opcodes, item1_position, item2_position, placement_position)
            index += 4
        else:
            print('error')
    if(list_of_opcodes[0] == 19690720):
        print(f'Answer is: {100 * tpl[0] + tpl[1]}')
    #part I - when tpl (12, 2) => print(list_of_opcodes[0]) ==> 2890696
    #part II - tpl (82, 26)

def generator():
    for i in range(100):
        for j in range(100):
            yield (i, j)


for i in generator():
    gravity_assist(i)

2 诇讬讬拽讬诐

砖讗诇讛 讘谞讜讙注 诇住讜祝 讛讞诇拽 讛专讗砖讜谉, 讛诐 专讜砖诪讬诐 砖讗讞专讬 砖注砖讬谞讜 讗转 讛诪注专讻转, 爪专讬讱 诇讛讞诇讬祝 讗转 讛诪住驻专讬诐
讗转 诪讬拽讜诐 1 讘注专讱 12 讜讗转 诪讬拽讜诐 2 讘注专讱 2. 讜讗讝 诇讘讚讜拽 讗讬讝讛 注专讱 谞讜转专 讘诪讬拽讜诐 0 诇讗讞专 住讬讜诐 讛转讜讻谞讬转.

讛讗诐 讛讻讜讜谞讛 讛讬讗 诇砖谞讜转 讗转 讛拽讜讘抓 砖讛诐 诪讘讬讗讬诐 诇谞讜, 讗讜 诇拽讞转 讗转 讛转讜爪讗讜转 讗讞专讬 讛专爪讛 讜讗讝 诇注砖讜转 砖讬谞讜讬 讜诇讛专讬抓 讗讜转诐 砖讜讘?

诇讗 讘讟讜讞 砖讛讘谞转讬 讗转 讛讞诇拽 讛讝讛

讗讞专讬 砖拽专讗转 讗转 讛诪住驻专讬诐 诪讛拽讜讘抓, 诇驻谞讬 砖讗转讛 注讜讘讚 注诇讬讛诐. 讻诇讜诪专 诇驻谞讬 讛讛专爪讛 砖诇 砖讗专 讛拽讜讚.
讘讙诇诇 砖驻住驻住转讬 讗转 讛砖讜专讛 讛讝讜砖爪专讬讱 诇讛讞诇讬祝 讛转驻住讟谞转讬 注诇 讞诇拽 1 专讘注 砖注讛 诪讬讜转专转 诇讙诪专讬 讘诇讬 诇讛讘讬谉 诇诪讛 讛拽讜讚 砖诇讬 诇讗 诪讞讝讬专 讗转 讛转砖讜讘讛 讛谞讻讜谞讛 :face_with_head_bandage:

诇讬讬拽 1

讝讛 讻诪讜 讗讘诇 砖讗谞讬 讗讻谞住 诇拽讜讘抓 注爪诪讜 讜讗砖谞讛 讗转 讝讛 砖诐 谞讻讜谉 ? 讗讜 砖讬砖 驻讛 诪砖讛讜 砖讗谞讬 诪驻住驻住

诇讬讬拽 1

诪讘讞讬谞转诐 讛注讬拽专 砖转讙讬注 诇诪住驻专 , 讗转讛 讗驻讬诇讜 诇讗 讞讬讬讘 诇注砖讜转 讝讗转 注诐 拽讜讘抓 - 讗谞讬 讛注转拽转讬 讗转 讛拽讜讘抓 诇转讜讱 诇讬住讟 讜讙诐 砖讬谞讬转讬 砖诐.

诇诪讛 砖诇讗 转砖谞讛 讘拽讜讚? 讝讛 专拽 诇讛讜住讬祝 砖讜专讛 讜讬讛驻讜讱 讗转 讛拽讜讚 诇讙谞专讬 讜诪转讗讬诐 诇讻诇 专爪祝 诪住驻专讬诐 :slight_smile:

2 诇讬讬拽讬诐

砖讬谞讜讬 讘拽讜讚 讘讗诪转 讬注砖讛 讗转 讝讛 讬讜转专 谞讻讜谉 诇拽专讗转 住注讬祝 2 , 讗讘诇 讻砖拽专讗转讬 讗转 住注讬祝 讗鈥 讝讛 讛讬讛 住转诐 谞专讗讛 诇讬 诪讟讜驻砖 讜讟专讞谞讬 讻诪讜 讻诇 住讬驻讜专 讛住讘转讗.

讗讬谉 诇讬 讘注讬讛 诇砖谞讜转 讘拽讜讚 讗谞讬 诪谞住讛 诇讛讘讬谉 讗诐 讝讛 讗讻谉 诪讛 砖爪专讬讱 诇注砖讜转鈥 讻讬 讝讛 诪讞讝讬专 诇讬 注讚讬谉 讗转 讗讜转讜 讛诪住驻专
砖讗谞讬 诪拽讘诇 讙诐 砖讗谞讬 诇讗 诪砖谞讛 鈥 讜讝讛 诪讜讝专 :slight_smile:

转砖讜讘讛- 讞诇拽 讗' + 讘'
# Part 1
def program_alarm(noun, verb, number_list):
    number_list[1] = noun
    number_list[2] = verb

    index = 0
    while number_list[index] != 99:
        current_row = number_list[index: index + 4]
        if current_row[0] == 1:
            tot =  number_list[current_row[1]] + number_list[current_row[2]]
            number_list[current_row[3]] = tot
        else:
            tot =  number_list[current_row[1]] * number_list[current_row[2]]
            number_list[current_row[3]] = tot

        index += 4
    return number_list


# Part 2
def get_noun_and_verb(number_list, number_to_get):
    noun = 0
    while noun <= 99:
        verb = 0
        while verb <= 99:
            number_list_copy = number_list.copy()
            number_list[1] = noun
            number_list[2] = verb
            index = 0
            while number_list_copy[index] != 99 and index <= len(number_list_copy):
                current_row = number_list_copy[index: index + 4]
                if current_row[0] == 1:
                    tot =  number_list_copy[current_row[1]] + number_list_copy[current_row[2]]
                    number_list_copy[current_row[3]] = tot

                else:
                    tot =  number_list_copy[current_row[1]] * number_list_copy[current_row[2]]
                    number_list_copy[current_row[3]] = tot

                if number_list_copy[0] == number_to_get:
                    return noun, verb
                index += 4
            verb += 1
        noun += 1

number_list = [1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,13,19,1,9,19,23,2,13,23,27,2,27,13,31,2,31,10,35,1,6,35,39,1,5,39,43,1,10,43,47,1,5,47,51,1,13,51,55,2,55,9,59,1,6,59,63,1,13,63,67,1,6,67,71,1,71,10,75,2,13,75,79,1,5,79,83,2,83,6,87,1,6,87,91,1,91,13,95,1,95,13,99,2,99,13,103,1,103,5,107,2,107,10,111,1,5,111,115,1,2,115,119,1,119,6,0,99,2,0,14,99]
print(program_alarm(12, 2, number_list))
print()
number_list = [1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,13,19,1,9,19,23,2,13,23,27,2,27,13,31,2,31,10,35,1,6,35,39,1,5,39,43,1,10,43,47,1,5,47,51,1,13,51,55,2,55,9,59,1,6,59,63,1,13,63,67,1,6,67,71,1,71,10,75,2,13,75,79,1,5,79,83,2,83,6,87,1,6,87,91,1,91,13,95,1,95,13,99,2,99,13,103,1,103,5,107,2,107,10,111,1,5,111,115,1,2,115,119,1,119,6,0,99,2,0,14,99]
noun_and_verb = get_noun_and_verb(number_list, 19690720)
print(100 * noun_and_verb[0] + noun_and_verb[1])
2 诇讬讬拽讬诐

转砖讜讘讜转: :star: :star:

讞诇拽 1
def get_inputs ():
    with open('resources/inputs.txt', 'r') as file:
        inputs = file.read().split(',')
    return list_as_ints(inputs)


def list_as_ints(inputs):
    for i in range(len(inputs)):
        inputs[i] = int(inputs[i])
    return inputs


def incode(inputs, location, command):
    position1 = inputs[location + 1]
    position2 = inputs[location + 2]
    output_position = inputs[location + 3]
    if command == 1:
        inputs[output_position] = inputs[position1] + inputs[position2]
    else:
        inputs[output_position] = inputs[position1] * inputs[position2]
    return inputs


def run_incode(inputs):
    inputs[1] = 12
    inputs[2] = 2
    length = len(inputs)
    for i in range(0, length, 4):
        command = inputs[i]
        if command == 99:
            return inputs
        inputs = incode(inputs, i, command)
    return inputs


inputs = get_inputs()
print(run_incode(inputs))
讞诇拽 2
def get_inputs ():
    with open('resources/inputs.txt', 'r') as file:
        inputs = file.read().split(',')
    return list_as_ints(inputs)


def list_as_ints(inputs):
    for i in range(len(inputs)):
        inputs[i] = int(inputs[i])
    return inputs


def incode(inputs, location, command):
    position1 = inputs[location + 1]
    position2 = inputs[location + 2]
    output_position = inputs[location + 3]
    if command == 1:
        inputs[output_position] = inputs[position1] + inputs[position2]
    else:
        inputs[output_position] = inputs[position1] * inputs[position2]
    return inputs


def run_incode(inputs, noun, verb):
    inputs[1] = noun
    inputs[2] = verb
    length = len(inputs)
    for i in range(0, length, 4):
        command = inputs[i]
        if command == 99:
            return inputs[0]
        inputs = incode(inputs, i, command)


def find_verb_and_noun(parameter):   
    original_input = get_inputs()
    is_found = False
    i = 0
    while i < 100 and not is_found:
        j = 0
        while j < 100 and not is_found:
            inputs = original_input.copy()
            result = run_incode(inputs, i, j)
            if result == parameter:
                is_found = True
            else:
                j += 1
        i += 1
    return (i - 1 ,j)


print(find_verb_and_noun(19690720))
诇讬讬拽 1

诪讬砖讛讜 讬讻讜诇 诇转专讙诐 诇住讬谞讬转 讗转 讛讞诇拽 讛砖谞讬, 诇讗 诪爪诇讬讞 诇讛讘讬谉 讜注讜讚 砖谞讬讬讛 讗谞讬 诪注讬祝 住讟讬谞讙专 注诇 讛讞诇诇讬转 砖诇 住谞讟讛

3 诇讬讬拽讬诐

谞讻讜谉 砖讘讞诇拽 1 讛讞诇驻转 讗转 诪拽讜诐 1 讜 2 讘诪住驻专讬诐 砖讘讬拽砖讜.
注讻砖讬讜 讗转讛 爪专讬讱 诇注砖讜转 拽讜讚 砖诪讝讬谉 注专讻讬诐 讘1 讜2 (讻诇 讗讞讚 诪讛诐 讬讻讜诇 诇讛讬讜转 诪0 注讚 99 讻讜诇诇)
注讚 砖转讙讬注 诇讻讱 砖讘诪拽讜诐 讛0 讬砖 讗转 讛转讜爪讗讛 砖讛诐 专砖诪讜 诇讱 砖诐.

转讙讬讚 诇讬 讗诐 讛讘谞转 讗讜 诇讗 诪拽住讬诪讜诐 讗谞住讛 诇讞讚讚

2 诇讬讬拽讬诐

砖讬讛讬讜 讘专讬讗讬诐鈥 讗讬讝讛 讚专讱 注拽讜诪讛 诇讛讙讬讚 诪砖驻讟 拽爪专 讻诇 讻讱鈥 转讜讚讛 注诇 讛讛住讘专!
讗讙讘, 讛诪住驻专 讘讞诇拽 讛砖谞讬 砖讬讜爪讗 讝讛 转讗专讬讱 讛爪注讚 讛专讗砖讜谉 注诇 讛讬专讞, 20.7.1969 :slight_smile:

5 诇讬讬拽讬诐

讻谉 讝讛 讘讛讞诇讟 讛讬讛 讻讘讚 诪讛 砖拽专讛 砖诐 !
讛转讗专讬讱 砖*讗讜诇讬 讛讙注谞讜 讘讜 诇讬专讞 :slight_smile: