コード例 #1
0
ファイル: day10.py プロジェクト: stubz151/Advent-Of-code
def get_value_second_try(arr):
    traversed_nodes_and_count = {len(arr) - 1: 1}
    for i, e in reversed(list(enumerate(arr[:-1]))):
        diff = arr[i + 1] - e
        if diff == 3:
            traversed_nodes_and_count[i] = traversed_nodes_and_count[i + 1]
        if diff == 2:
            traversed_nodes_and_count[i] = traversed_nodes_and_count[i + 1]
            if 0 <= i - 2 and arr[i + 2] - e == 3:
                traversed_nodes_and_count[i] = traversed_nodes_and_count[
                    i] + traversed_nodes_and_count[i + 2]
        if diff == 1:
            traversed_nodes_and_count[i] = traversed_nodes_and_count[i + 1]
            if arr[i + 2] - e == 2:
                traversed_nodes_and_count[i] = traversed_nodes_and_count[
                    i] + traversed_nodes_and_count[i + 2]
                if arr[i + 3] - e == 3:
                    traversed_nodes_and_count[i] = traversed_nodes_and_count[
                        i] + traversed_nodes_and_count[i + 3]

    num_to_return = traversed_nodes_and_count[0] + traversed_nodes_and_count[1]
    return num_to_return


if __name__ == '__main__':
    sorted_list = sorted(CommonLogic.get_input_as_int_arr())
    sorted_list.append(sorted_list[-1] + 3)
    sorted_list.insert(0, 0)
    print('first question: ' + str(get_value(sorted_list)))
    print('second question: ' + str(get_value_second_try(sorted_list)))
コード例 #2
0
ファイル: day13.py プロジェクト: stubz151/Advent-Of-code
            if i == 0:
                value = int(x) * attempt
                continue
            if x == 'x':
                continue
            if (value + dict_num_pos[x]) % int(x) != 0:
                break
        else:
            return value


##chinese number theory and googling got me here
def second_question_trytwo(arr):
    busses = ["x" if x == "x" else int(x) for x in arr[1].split(",")]
    mods = {bus: -i % bus for i, bus in enumerate(busses) if bus != "x"}
    print(mods)
    vals = list(reversed(sorted(mods)))
    val = mods[vals[0]]
    r = vals[0]
    for b in vals[1:]:
        while val % b != mods[b]:
            val += r
        r *= b
    return val


if __name__ == '__main__':
    arr = CommonLogic.get_input_as_str_arr()
    #print('first question: ' + str(get_value(arr)))
    print('second question: ' + str(second_question_trytwo(arr)))
コード例 #3
0
from common_logic import CommonLogic


def get_value(arr):
    pass


def get_value_second_try(arr):
    pass


if __name__ == '__main__':
    arr = CommonLogic.get_input_as_int_arr()
    print('first question: ' + str(get_value(arr)))
    print('second question: ' + str(get_value_second_try(arr)))
コード例 #4
0
ファイル: day5.py プロジェクト: stubz151/Advent-Of-code
        if val == 'L':
            num_to_min = pow(2, len(number) - i - 8)
            starting_row -= num_to_min
    return (starting_num * 8) + starting_row


def get_highest_value(input_arr):
    highest_num = get_value(input_arr[0])
    for x in input_arr:
        if get_value(x) > highest_num:
            highest_num = get_value(x)
    return highest_num


def get_seat(input_arr):
    seat_arr = []
    for x in input_arr:
        seat_arr.append(get_value(x))
    seat_arr.sort()
    seat_number = -1
    print(seat_arr)
    for i, x in enumerate(seat_arr[:len(seat_arr) - 1]):
        if seat_arr[i + 1] != x + 1:
            seat_number = x + 1
            break
    return seat_number


if __name__ == '__main__':
    print(get_seat(CommonLogic.get_input_as_str_arr()))
コード例 #5
0
ファイル: day9.py プロジェクト: stubz151/Advent-Of-code
            if x + y == val:
                if inner_index != i:
                    return True
    return False


def get_value(arr):
    pre_amble_arr = arr[0:25]
    for x in arr[25:]:
        if check_value_is_in_preamble(pre_amble_arr, x):
            pre_amble_arr.append(x)
            pre_amble_arr = pre_amble_arr[1:]
        else:
            return x


def get_value_second_question(arr):
    first_weakness = get_value(arr)
    for i, x in enumerate(arr):
        arr_of_adders = []
        for y in arr[i:]:
            if sum(arr_of_adders) > first_weakness:
                break
            if sum(arr_of_adders) == first_weakness:
                return min(arr_of_adders) + max(arr_of_adders)
            arr_of_adders.append(y)


if __name__ == '__main__':
    print(get_value_second_question(CommonLogic.get_input_as_int_arr()))
コード例 #6
0
from common_logic import CommonLogic


def get_value(input_arr):
    for first_i, first_value in enumerate(input_arr):
        split_arr = input_arr[first_i + 1:len(input_arr)]
        for second_i, second_value in enumerate(split_arr):
            second_split_arr = input_arr[second_i + 1:len(split_arr)]
            for third_i, third_value in enumerate(second_split_arr):
                if first_value + second_value + third_value == 2020:
                    return first_value * second_value * third_value


if __name__ == '__main__':
    print(get_value(CommonLogic.get_input_as_int_arr()))
コード例 #7
0
ファイル: day3.py プロジェクト: stubz151/Advent-Of-code
def loop_find_trees_count(x_skip, y_skip, input_arr):
    count = 0
    y = 0
    x = 0
    while y < len(input_arr):
        value = input_arr[y]
        while x + x_skip > len(value):
            value = value + value
        if value[x] == '#':
            count += 1
        x += x_skip
        y += y_skip
    return count


def get_value(input_arr):
    return loop_find_trees_count(3, 1, input_arr)


def get_value_second_challenge(input_arr):
    right1down1 = loop_find_trees_count(1, 1, input_arr)
    right3down1 = loop_find_trees_count(3, 1, input_arr)
    Right5down1 = loop_find_trees_count(5, 1, input_arr)
    right7down1 = loop_find_trees_count(7, 1, input_arr)
    right1down2 = loop_find_trees_count(1, 2, input_arr)
    return right1down1 * right1down2 * right7down1 * right3down1 * Right5down1


if __name__ == '__main__':
    print(get_value_second_challenge(CommonLogic.get_input_as_str_arr()))
コード例 #8
0
                tags_in_line_count += 1
            elif tags == 'iyr' and 2010 <= int(value_for_tag) <= 2020:
                tags_in_line_count += 1
            elif tags == 'eyr' and 2020 <= int(value_for_tag) <= 2030:
                tags_in_line_count += 1
            elif tags == 'hgt':
                m = re.split('(\d+)', value_for_tag)
                if m[2] == 'cm' and 150 <= int(m[1]) <= 193:
                    tags_in_line_count += 1
                elif m[2] == 'in' and 59 <= int(m[1]) <= 76:
                    tags_in_line_count += 1
            elif tags == 'hcl' and value_for_tag[0] == '#' and len(
                    value_for_tag) == 7 and hclR.match(value_for_tag[:1]):
                tags_in_line_count += 1
            elif tags == 'ecl' and (
                    value_for_tag == 'amb' or value_for_tag == 'blu'
                    or value_for_tag == 'brn' or value_for_tag == 'gry'
                    or value_for_tag == 'hzl' or value_for_tag == 'oth'
                    or value_for_tag == 'grn'):
                tags_in_line_count += 1
            elif tags == 'pid' and len(value_for_tag) == 9:
                tags_in_line_count += 1

        if tags_in_line_count >= 7:
            count += 1
    return count


if __name__ == '__main__':
    print(get_value_second_test(CommonLogic.get_input_as_batches()))
コード例 #9
0
ファイル: day6.py プロジェクト: stubz151/Advent-Of-code
from common_logic import CommonLogic


def get_count(arr):
    count = 0
    for x in arr:
        x = x.replace(" ", "")
        myset = set(x)
        count += len(myset)
    return count


def get_count_second_question(arr):
    count = 0
    for x in arr:
        line = x.split(' ')
        for y in line[0]:
            if (x.count(y)) == len(line):
                count += 1
    return count


if __name__ == '__main__':
    print(get_count_second_question(CommonLogic.get_input_as_batches()))