Beispiel #1
0
def f1():
    for num1 in numbers:
        num2 = 2020 - num1

        if num2 in numbers:
            print(num1, num2, num1 * num2)

            return


def f2():
    for num1 in numbers:
        for num2 in numbers:
            num3 = 2020 - (num1 + num2)

            if num3 in numbers:
                print(num1, num2, num3, num1 * num2 * num3)

                return


from input_loader import load_input

numbers = sorted([int(num) for num in load_input().split()])

f1()
f2()
Beispiel #2
0
                    return False
            elif key == 'pid':
                if len(val) != 9 or int(val) < 0:
                    return False
        except Exception as e:
            print(key, e)
            return False

    return True


def f1(passports: List[dict], needed_keys: List[str]) -> None:
    print('f1:',
          len([p for p in passports if is_passport_valid_v1(p, needed_keys)]))


def f2(passports: List[dict], needed_keys: List[str]) -> None:
    print('f1:',
          len([p for p in passports if is_passport_valid_v2(p, needed_keys)]))


from input_loader import load_input

inp = load_input()
passport_strs = [p.strip().replace('\n', ' ') for p in inp.split('\n\n')]
passports = [parse_passport(passport_str) for passport_str in passport_strs]

needed_keys = ['ecl', 'pid', 'eyr', 'hcl', 'byr', 'iyr', 'hgt']

f1(passports, needed_keys)
f2(passports, needed_keys)
Beispiel #3
0
    # score = 0

    # for group in groups:
    #     needed_count = len(group)

    #     for count in get_group_occurences(group).values():
    #         if count == needed_count:
    #             score += 1

    # print('f2:', score)

    # SHORT
    print(
        'f2:',
        sum([
            len([
                '*' for answer_count in occurence.values()
                if answer_count == responders
            ]) for responders, occurence in [(len(group),
                                              get_group_occurences(group))
                                             for group in groups]
        ]))


from input_loader import load_input

groups = [[[str(c) for c in group_line] for group_line in group.split('\n')]
          for group in load_input().split('\n\n')]

f1(groups)
f2(groups)
Beispiel #4
0
        used_positions.append(i)
        cmd, value = commands[i]
        last_i = i

        if cmd == 'acc':
            accumulator += value
        elif cmd == 'jmp':
            i += value

            continue

        i += 1

    return accumulator


def f1(commmands: List[Tuple[str, int]]) -> None:
    print(run(commmands, correct_duplicate=False))


def f2(commmands: List[Tuple[str, int]]) -> None:
    print(run(commmands, correct_duplicate=True))


from input_loader import load_input

commands = [parse_command(cmd.strip()) for cmd in load_input().split('\n')]

f1(commands)
f2(commands)
Beispiel #5
0
    for start_pos in range(pos):
        for end_pos in range(start_pos+3, pos+1):
            if sum(numbers[start_pos:end_pos]) == num:
                sorted_val = sorted(numbers[start_pos:end_pos])
                print('f2_v1:', sorted_val[0] + sorted_val[-1])

                return

def f2_v2(numbers: List[int], sample_size: int) -> None:
    pos, num = find_contagious_num(numbers, sample_size)

    for i in range(3, pos):
        l = pos - i

        for start_pos in range(pos-l):
            end_pos = start_pos + l

            if sum(numbers[start_pos:end_pos]) == num:
                sorted_val = sorted(numbers[start_pos:end_pos])
                print('f2_v2:', sorted_val[0] + sorted_val[-1])

                return

from input_loader import load_input

NUMBERS = [int(num_str.strip()) for num_str in load_input().split('\n')]
SAMPLE_SIZE = 25

f1(NUMBERS, SAMPLE_SIZE)
f2_v1(NUMBERS, SAMPLE_SIZE)
f2_v2(NUMBERS, SAMPLE_SIZE)
Beispiel #6
0
    invalid = []

    for l in lines:
        comps = l.split(':')
        pw = comps[1].strip()
        rules = comps[0].split(' ')
        rule_char = rules[1]
        rule_char_count = rules[0].split('-')
        rule_char_pos_1 = int(rule_char_count[0]) - 1
        rule_char_pos_2 = int(rule_char_count[1]) - 1

        contains_pos_1 = len(pw) > rule_char_pos_1 and str(
            pw[rule_char_pos_1]) == rule_char
        contains_pos_2 = len(pw) > rule_char_pos_2 and str(
            pw[rule_char_pos_2]) == rule_char

        if contains_pos_1 ^ contains_pos_2:
            valid.append(pw)
        else:
            invalid.append(pw)

    print('f2: valid  ', len(valid))
    print('f2: invalid', len(invalid))


from input_loader import load_input

lines = load_input().split('\n')

f1(lines)
f2(lines)
Beispiel #7
0
    print('f1: max_seat_id =', max_seat_id)


def f2(seat_codes: List[str], row_count: int, column_count: int) -> None:
    seats = [[0 for _ in range(column_count)] for _ in range(row_count)]

    for seat_code in seat_codes:
        row = get_pos([str(s) for s in seat_code[:7]], row_count)
        column = get_pos([str(s) for s in seat_code[7:]], column_count)
        seats[row][column] = 1

    missing_positions = [[(row, column) for column in range(column_count)
                          if seats[row][column] == 0]
                         for row in range(row_count)]
    missing_positions = [row for row in missing_positions if len(row) == 1]
    missing_position = missing_positions[0][0]

    print('f2: missing_position =', missing_position, 'id =',
          missing_position[0] * 8 + missing_position[1])


from input_loader import load_input

seat_codes = load_input().split('\n')

ROW_COUNT = 128
COLUMN_COUNT = 8

f1(seat_codes, ROW_COUNT, COLUMN_COUNT)
f2(seat_codes, ROW_COUNT, COLUMN_COUNT)
Beispiel #8
0
    return _parent_colors


def get_bag_count(rules: dict, bag_color: str) -> int:
    child_colors_counts = rules[bag_color]
    count = 0

    for child_bag_color, child_bag_count in rules[bag_color].items():
        count += child_bag_count + child_bag_count * get_bag_count(
            rules, child_bag_color)

    return count


def f1(rules: dict, target_bag_color: str) -> None:
    print(len(get_parents(rules, target_bag_color)))


def f2(rules: dict, target_bag_color: str) -> None:
    print(get_bag_count(rules, target_bag_color))


from kcu import kjson
from input_loader import load_input

rules = get_rules(load_input().split('\n'))
TARGET_BAG_COLOR = 'shiny gold'

f1(rules, TARGET_BAG_COLOR)
f2(rules, TARGET_BAG_COLOR)