Exemple #1
0
def part_two(path, day):
    # declarations_string = declarations.split('\n\n')
    # collect answers where each element in list contains all answers from the group, individual member answers are
    # separated by '\n'
    declarations_file = read_file(f"{path}\\{day}\\declarationforms",
                                  empty_lines=True)
    # collect all answers but do not smash, also collect number of persons in group, put tuples in list
    all_answers = [(list([y for y in x.replace('\n', '')]), len(x.split('\n')))
                   for x in declarations_file]
    # count the same answers
    count_same_answer = list()
    for groupanswers in all_answers:  # groupanswers ([<all groupanswers>], <size of group>)
        answer_occurence = Counter(
            groupanswers[0]
        )  # for all groupanswers count the occurence of each element
        nr_persons = groupanswers[1]  # number of people in group
        group_result = list()  # collect same answer given by group
        for key in answer_occurence:
            if answer_occurence[
                    key] == nr_persons:  # if the occurence of anser matches the number of people in group
                group_result.append(key)
        if group_result:  # if the group has given same answers
            count_same_answer.append(len(group_result))
    print(
        f"Solution {day} part 2: Sum of count same answer = {sum(count_same_answer)}"
    )
Exemple #2
0
def part_two(path, day):
    # s_bagrules_string = second_bagrules.split('\n')
    bagrules_file = read_file(f"{path}\\{day}\\bagrules", as_lines=True)
    baglist = split_stringlist(bagrules_file, 'bag')
    baglist_sanitized = sanitize_baglist(baglist)
    bagrules_guide = bagrules_dict(baglist_sanitized)
    get_bags = count_bags(bagrules_guide, {'shiny gold': 1})
    print(
        f"Solution {day} part 2: {get_bags - 1} individual bags required for 1 shiny gold bag."
    )
Exemple #3
0
def part_one(path, day):
    # bagrules_string = bagrules.split('\n')
    bagrules_file = read_file(f"{path}\\{day}\\bagrules", as_lines=True)
    baglist = split_stringlist(bagrules_file, 'bag')
    baglist_sanitized = sanitize_baglist(baglist)
    bagrules_guide = bagrules_dict(baglist_sanitized)
    get_bags = find_more_bags(bagrules_guide, ['shiny gold'])
    print(
        f"Solution {day} part 1: {len(get_bags) - 1} bag colors containing at least 1 shiny gold bag."
    )
Exemple #4
0
def part_one(path, day):
    # boarding_passes_string = boarding_passes.split('\n')
    # bin_boarding_passes_s = bin_conversion(boarding_passes_string)
    boarding_passes_file = read_file(f"{path}\\{day}\\boardingpasses",
                                     as_lines=True)
    bin_boarding_passes = bin_conversion(boarding_passes_file)
    bin_flipped = bin_flip(bin_boarding_passes)
    row_column_db = get_row_column(bin_flipped)
    seat_id_db = get_seat_id(row_column_db)
    print(f"Solution {day} part 1: Highest seat id = {max(seat_id_db)}")
Exemple #5
0
def part_two(path, day):
    """
    yield solution for day 1 part 1
    :param path: file location
    :param day: day
    :return: print solution
    """
    comb = get_combinations(read_file(f"{path}\\{day}\\numbers", as_int=True),
                            3)
    sum_comb = sum_combinations(comb, 2020)
    print(f"Solution {day} part 2: {multiply_nrs(sum_comb)}")
Exemple #6
0
def part_two(path, day):
    # data = passwords.split('\n')
    data = read_file(f"{path}\\{day}\\passwords", as_lines=True)
    valid_passwd = int()
    for entry in data:
        p = get_parameters(entry)
        valid = valid_occurence_posminmax(p['passwd'], p['letter'],
                                          p['min_oc'], p['max_oc'])
        if valid:
            valid_passwd += 1
    print(f"Solution {day} part 2: {valid_passwd} valid password(s)")
Exemple #7
0
def part_one(path, day):
    # declarations_string = declarations.split('\n\n')
    # collect answers where each element in list contains all answers from the group, individual member answers are
    # separated by '\n'
    declarations_file = read_file(f"{path}\\{day}\\declarationforms",
                                  empty_lines=True)
    # smash all individual answers into unique (set) given answer and count the char occurence
    count_occurrences = [
        len(set([y for y in x.replace('\n', '')])) for x in declarations_file
    ]
    # sum all list items and print
    print(f"Solution {day} part 1: Sum of counts = {sum(count_occurrences)}")
Exemple #8
0
def part_two(path, day):
    boarding_passes_file = read_file(f"{path}\\{day}\\boardingpasses",
                                     as_lines=True)
    bin_boarding_passes = bin_conversion(boarding_passes_file)
    bin_flipped = bin_flip(bin_boarding_passes)
    row_column_db = get_row_column(bin_flipped)
    seat_id_db = get_seat_id(row_column_db)
    all_seat_ids = set(
        [x for x in range(min(seat_id_db),
                          max(seat_id_db) + 1)])
    my_seat_id = all_seat_ids - set(seat_id_db)
    print(f"Solution {day} part 2: My seat id = {my_seat_id}")
Exemple #9
0
def part_two(path, day):
    raw_bootcode_s = bc.split('\n')
    raw_bootcode_f = read_file(f"{path}\\{day}\\bootcode", as_lines=True)
    bootcode = sanitize_bootcode(raw_bootcode_f)
    swap_inst = [('jmp', 'nop'), ('nop', 'jmp')]
    for inst in swap_inst:
        si_index = get_instr_index(bootcode, inst[0])
        for si in si_index:
            copy_bootcode = copy.deepcopy(bootcode)
            copy_bootcode[si]['c']['instr'] = inst[1]
            result = run_bootcode(copy_bootcode, 2)
            if result:
                print("=====")
                print(f"swap: {inst[0]} for {inst[1]} at index: {si}")
                return
Exemple #10
0
def part_one(path, day):
    # raw_encoding_s = enc.split('\n')
    raw_encoding_f = read_file(f"{path}\\{day}\\encoding", as_lines=True)
    # encoding_s = [int(x) for x in raw_encoding_s]
    encoding_f = [int(x) for x in raw_encoding_f]
    # preamble_list = encoding_s[:5]
    # rest_list = encoding_s[5:]
    preamble_list = encoding_f[:25]
    rest_list = encoding_f[25:]

    while rest_list:
        sum_entry = rest_list.pop(0)
        preamble_comb = list(combinations(preamble_list, 2))
        preamble_sum = check_preamble_sum(preamble_comb, sum_entry)
        if preamble_sum:
            preamble_list.pop(0)
            preamble_list.append(sum_entry)
        else:
            print(f"Number: {sum_entry} cannot be formed by any combination of {preamble_list}!")
            return sum_entry
Exemple #11
0
def part_two(path, day):
    # raw_encoding_s = enc.split('\n')
    raw_encoding_f = read_file(f"{path}\\{day}\\encoding", as_lines=True)
    # encoding_s = [int(x) for x in raw_encoding_s]
    encoding_f = [int(x) for x in raw_encoding_f]
    # preamble_list = encoding_s[:5]
    # rest_list = encoding_s[5:]
    preamble_list = encoding_f[:25]
    rest_list = encoding_f[25:]
    # invalid_number = 88311122
    invalid_number = part_one(path, day)
    found = False
    while not found and rest_list:
        preamble_comb = get_combinations(preamble_list)
        check_sum = check_preamble_sum2(preamble_comb, invalid_number)
        if check_sum:
            check_sum.sort()
            print(f"sum of 1st and last contiguous range: {check_sum[0] + check_sum[-1]}, "
                  f"preamble list:{check_sum}")
            found = True
        else:
            preamble_list.pop(0)
            preamble_list.append(rest_list.pop(0))
Exemple #12
0
def part_one(path, day):
    raw_bootcode_s = bc.split('\n')
    raw_bootcode_f = read_file(f"{path}\\{day}\\bootcode", as_lines=True)
    bootcode = sanitize_bootcode(raw_bootcode_f)
    run_bootcode(bootcode, 1)