Advent of Code 2019 🤯 (יום 2)

תגיות: ,

בטח שלא, היית מעיף עליהם סטינגר

5 לייקים

ללא ספק ההוראות מתישות, אבל אתגר מגניב!
אני מקווה שאני אמצא לטרמינולוגיה הזו שימוש בהמשך :sleeping:

# ADVENT OF CODE, DAY 2, Puzzle 1,2

def get_intcode():
    with open('input.txt', 'r') as file:
        intcode = file.read().split(',')
    intcode = intcode_items_to_int(intcode)
    return intcode


def restore_gravity(intcode, noun, verb):
    intcode[1] = noun
    intcode[2] = verb
    return intcode


def intcode_items_to_int(intcode):
    for i, num in enumerate(intcode):
        intcode[i] = int(intcode[i])
    return intcode


def run_intcode(verb, noun):
    intcode = get_intcode()
    intcode = restore_gravity(intcode, verb, noun)
    for i in range(0, len(intcode), 4):
        num1, num2 = intcode[intcode[i + 1]], intcode[intcode[i + 2]]
        if intcode[i] == 1:
            intcode[intcode[i + 3]] = num1 + num2
        if intcode[i] == 2:
            intcode[intcode[i + 3]] = num1 * num2
        if intcode[i] == 99:
            return intcode


def find_correct_parameters(output):
    intcode = get_intcode()
    for verb in range(0, 100):
        for noun in range(0, 100):
            intcode = run_intcode(verb, noun)
            if intcode[0] == output:
                return verb, noun


print(find_correct_parameters(19690720))
print(run_intcode(20, 3))
לייק 1
5 לייקים

מיותר לציין שהאתגרים האלו כיפיים ותרגול מצויין! רק ההוראות היו מתישות :slight_smile:

2 לייקים

נראה מעולה ! שים לב לבעיה קטנה והיא שהקוד שלך רץ מ 0 עד 98 ולא עד 99 כמו שנדרש בתרגיל :slight_smile:

לייק 1

צודק! תוקן.
אל יתהלל חוגר כמפתח (בפייתון?) :stuck_out_tongue:

לייק 1

חלק 2 כמעט חיסל לי את המוח :joy:, מסקנה: לא לקבל הנחיות משדונים
חלק 1

#day2
#part1
import operator


def get_intcode_program(intcode):
    with open(intcode, "r") as intcode_program:
        intcode_program_read = intcode_program.read()
    return intcode_program_read.split(",")


def alter_to_1202_program_alarm(intcode):
    my_intcode_program_1202 = get_intcode_program(intcode)
    my_intcode_program_1202[1] = 12
    my_intcode_program_1202[2] = 2
    return my_intcode_program_1202


def opcode(code, a, b):
    if code == 1:
        return operator.add(a, b)
    elif code == 2:
        return operator.mul(a, b)


def run_program(intcode):
    my_intcode_program = alter_to_1202_program_alarm(intcode)
    for position in range(len(my_intcode_program) // 4):
        operation = int(my_intcode_program[position * 4])
        if operation == 99:
            return my_intcode_program[0]
        first_pos = int(my_intcode_program[position * 4 + 1])
        second_pos = int(my_intcode_program[position * 4 + 2])
        third_pos = int(my_intcode_program[position * 4 + 3])
        my_intcode_program[third_pos] = opcode(operation, int(my_intcode_program[first_pos]), int(my_intcode_program[second_pos]))
    return my_intcode_program[0]


print(run_program("resources//day2.txt"))

חלק 2

#day2
#part1
import operator


def get_intcode_program(intcode):
    with open(intcode, "r") as intcode_program:
        intcode_program_read = intcode_program.read()
    return intcode_program_read.split(",")


def alter_to_1202_program_alarm(intcode, noun, verb):
    my_intcode_program_1202 = get_intcode_program(intcode)
    my_intcode_program_1202[1] = noun
    my_intcode_program_1202[2] = verb
    return my_intcode_program_1202


def opcode(code, a, b):
    if code == 1:
        return operator.add(a, b)
    elif code == 2:
        return operator.mul(a, b)


def run_program(intcode, noun, verb):
    my_intcode_program = alter_to_1202_program_alarm(intcode, noun, verb)
    for position in range(len(my_intcode_program) // 4):
        operation = int(my_intcode_program[position * 4])
        if operation == 99:
            return my_intcode_program[0]
        first_pos = int(my_intcode_program[position * 4 + 1])
        second_pos = int(my_intcode_program[position * 4 + 2])
        third_pos = int(my_intcode_program[position * 4 + 3])
        my_intcode_program[third_pos] = opcode(operation, int(my_intcode_program[first_pos]), int(my_intcode_program[second_pos]))
    return my_intcode_program[0]


def noun_verb_possibilities(intcode):
    for noun in range(100):
        for verb in range(100):
            if run_program(intcode, noun, verb) == 19690720:
                return 100 * noun + verb


print(noun_verb_possibilities("resources//day2.txt"))
2 לייקים
תשובה
def intcode_input(path, changeforindex1, changeforindex2):
    with open(path, "r") as f:
         codes = f.read().split(",")
    
    list_of_codes = []
    for char in codes:
        list_of_codes.append(int(char))
    list_of_codes[1] = changeforindex1  # Position 1
    list_of_codes[2] = changeforindex2  # Position 2
    return list_of_codes


def split_input_to_lines(intcode):
    for index, item in enumerate(intcode):
        if not index % 4 and item == 99:
            return intcode
        if not index % 4 and item == 1:
            intcode[intcode[index + 3]] = intcode[intcode[index + 1]] + intcode[intcode[index + 2]]
        elif not index % 4 and item == 2:
            intcode[intcode[index + 3]] = intcode[intcode[index + 1]] * intcode[intcode[index + 2]]
    return intcode


def all_intcode_combinations(path):
    for i in range(100):
        for j in range(100):
            result = split_input_to_lines(intcode_input(path, i, j))
            if result[0] == 19690720:
                return i * 100 + j


print(split_input_to_lines(intcode_input("save-santa-day2.txt", 12, 2)))  # Part 1
print(all_intcode_combinations("save-santa-day2.txt"))  # Part 2
4 לייקים
def intcode_computer(opcode):
temp_opcode = opcode.copy()
index = 0
state = temp_opcode[index]
while state != 99:
    input_1 = temp_opcode[index + 1]
    input_2 = temp_opcode[index + 2]
    output = temp_opcode[index + 3]
    if state == 1:
        temp_opcode[output] = temp_opcode[input_1] + temp_opcode[input_2]
    elif state == 2:
        temp_opcode[output] = temp_opcode[input_1] * temp_opcode[input_2]
    index += 4
    state = temp_opcode[index]

return temp_opcode


def find_inputs(opcode, wanted_output):
temp_opcode = opcode.copy()
for noun in range(100):
    temp_opcode[1] = noun
    for verb in range(100):
        temp_opcode[2] = verb
        output = intcode_computer(temp_opcode)[0]
        if output == wanted_output:
            return noun, verb


with open('resources/advent/Day 2.txt', 'r') as file_handler:
opcode = file_handler.read().split(',')

for i in opcode:
opcode[opcode.index(i)] = int(i)

find_inputs(opcode, 19690720)
לייק 1

טוף, אז הרשיתי לעצמי להשתמש בחומרים שלמדנו השבוע :wink:

from itertools import count
import operator


OPCODE_SIZE = 4
OPCODES_FUNCTIONS = {
    1: operator.add,
    2: operator.mul,
}


def get_input():
    with open('input.txt', 'r') as challenge_input:
        return list(map(int, challenge_input.read().strip().split(',')))


def instructions_generator(program):
    for i in count(0, OPCODE_SIZE):
        if program[i] == 99:
            return
        yield program[i:i + OPCODE_SIZE]


def get_output(program, a = 0, b = 0):
    program[1:3] = a, b
    for opcode, src1, src2, dst in instructions_generator(program):
        if any(len(program) <= param for param in (dst, src1, src2)):
            return None
        function = OPCODES_FUNCTIONS[opcode]
        program[dst] = function(program[src1], program[src2])
    return program[0]


# Part 1
program = get_input()
print(get_output(list(program), 12, 2))


# Part 2
MAX_GUESS = 1000
WANTED_OUTPUT = 19_690_720
print(next(
    noun * 100 + verb
    for noun in range(MAX_GUESS) for verb in range(MAX_GUESS)
    if get_output(list(program), noun, verb) == WANTED_OUTPUT
))
2 לייקים

האם נפתח אתגר יום 3? :slightly_smiling_face:

ניתן עוד לפחות יום (אולי יומיים) כדי לאפשר גם לשאר להצטרף :slight_smile:

ים השתמשת בmap ! כנראה שתצטרך להגיש תרגיל השלמה
אלו חוקי הפורמט

מותררררר זה ב־builtins :@

2 לייקים

מחר נפרסם את אתגר יום 3 – היכונו!

לייק 1
def calc(lst):
    i = 0
    flag = True
    while i < len(lst) and flag:
        item = lst[i]
        if item == 1:
            position_of_num1 = lst[i+1]
            position_of_num2 = lst[i+2]
            postion_of_result = lst[i+3]
            lst[postion_of_result] = lst[position_of_num1] + lst[position_of_num2]
            i = i + 4
        elif item == 2:
            position_of_num1 = lst[i+1]
            position_of_num2 = lst[i+2]
            postion_of_result = lst[i+3]
            lst[postion_of_result] = lst[position_of_num1] * lst[position_of_num2]
            i = i + 4
        elif item == 99:
            flag = False
    return lst



print(calc([1,9,10,3,2,3,11,0,99,30,40,50]))
print(calc([1,0,0,0,99]))
print(calc([2,3,0,3,99]))
print(calc([2,4,4,5,99,0]))
print(calc([1,1,1,4,99,5,6,0,99]))
print(calc([1,12,2,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,19,10,23,2,10,23,27,1,27,6,31,1,13,31,35,1,13,35,39,1,39,10,43,2,43,13,47,1,47,9,51,2,51,13,55,1,5,55,59,2,59,9,63,1,13,63,67,2,13,67,71,1,71,5,75,2,75,13,79,1,79,6,83,1,83,5,87,2,87,6,91,1,5,91,95,1,95,13,99,2,99,6,103,1,5,103,107,1,107,9,111,2,6,111,115,1,5,115,119,1,119,2,123,1,6,123,0,99,2,14,0,0]))
לייק 1

חלקים 1 ו 2 ביחד:

def get_input(path):
    with open(path, 'r') as puzzle_input:
        temp_input = puzzle_input.read().split(',')
        return [int(x) for x in temp_input]


def make_program(puzzle_input, op_dict):
    op_code = 0
    i = 0
    while i < len(puzzle_input) - 4 and puzzle_input[i] != 99:
        op_code = puzzle_input[i]
        puzzle_input = handle_op_code(op_code, i, puzzle_input, op_dict)
        i += 4
        if puzzle_input == -1:
            return -1
        
    return puzzle_input


def handle_op_code(op_code, op_code_idx, puzzle_input, op_dict):
    op = get_operation(op_code)
    if op == 99:
        return puzzle_input
    elif op == -1:
        return puzzle_input
    
    if (puzzle_input[op_code_idx + 1] < len(puzzle_input)
        and puzzle_input[op_code_idx + 2] < len(puzzle_input)
        and puzzle_input[op_code_idx + 3] < len(puzzle_input)):
        first_input_idx = puzzle_input[op_code_idx + 1]
        second_input_idx = puzzle_input[op_code_idx + 2]
        output_idx = puzzle_input[op_code_idx + 3]
        puzzle_input[output_idx] = op_dict.get(op)(puzzle_input[first_input_idx], puzzle_input[second_input_idx])
        return puzzle_input
    
    else: 
        return -1


def get_operation(op_code):
    if op_code == 1:
        return '+'
    elif op_code == 2:
        return '*'
    elif op_code == 99:
        return 99
    else:
        return -1


def add(num1, num2):
    return num1 + num2


def multiply(num1, num2):
    return num1 * num2


def part_one(puzzle_input, op_dict, noun=12, verb=2):
    puzzle_input[1] = noun
    puzzle_input[2] = verb
    program = make_program(puzzle_input, op_dict)
    if program != -1:
        return program[0]


def part_two(puzzle_input, op_dict, num_at_pos_zero):
    noun = 0
    verb = 0
    for noun in range(100):
        for verb in range(100):
            if part_one(puzzle_input.copy(), op_dict, noun, verb) == num_at_pos_zero:
                return 100 * noun + verb
    return -1


def main():
    puzzle_input = get_input('resources/input2a.txt')
    op_dict = op_dict = {'+': add, '*': multiply}
    
    part_one_result = part_one(puzzle_input.copy(), op_dict)
    print(f'Part one result: {part_one_result}')
    
    part_two_result = part_two(puzzle_input.copy(), op_dict, num_at_pos_zero=19690720)
    print(f'Part two result: {part_two_result}')


main()
לייק 1

@ori81510

3 לייקים

חלק 2 משום מה נתקע על משהו אינסופי ולא מפסיק לרוץ :pleading_face:

לייק 1

בדרך כלל קל למצוא איפה יש לולאה אינסופית באמצעות הדפסות בכל מני מקומות בקוד. אם לא תמצאי את יכולה לשלוח פה, לקבל פידבק (ממני או מאחרים) ולערוך לפיו.

2 לייקים