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: