def main():
    config = Config(day=7)

    # PART ONE
    path_file = os.path.join(config.path_data, "bag_color_rules_test.txt")
    data_test = read_txt_file(path_file=path_file)
    data_test_parsed = parse_rules(data=data_test)
    bags_carrying_shiny_gold = find_gold_carrying_bags(data=data_test_parsed, bag_type="shiny gold bag")
    n_bags_carrying_shiny_gold = len(bags_carrying_shiny_gold)
    assert 4 == n_bags_carrying_shiny_gold

    path_file = os.path.join(config.path_data, "bag_color_rules.txt")
    data = read_txt_file(path_file=path_file)
    data_parsed = parse_rules(data=data)
    bags_carrying_shiny_gold = find_gold_carrying_bags(data=data_parsed)
    assert 128 == len(bags_carrying_shiny_gold)

    # PART TWO
    bags_unpacked = unpack(rules=data_test_parsed, inner_bags=data_test_parsed["shiny gold bags"])
    assert 32 == len(bags_unpacked)

    path_file = os.path.join(config.path_data, "bag_color_rules_test_part_two.txt")
    data_test_two = read_txt_file(path_file=path_file)
    data_test_two_parsed = parse_rules(data=data_test_two)
    bags_unpacked = unpack(rules=data_test_two_parsed, inner_bags=data_test_two_parsed["shiny gold bags"])
    assert 126 == len(bags_unpacked)

    bags_unpacked = unpack(rules=data_parsed, inner_bags=data_parsed["shiny gold bags"])
    print(f"The total number of bags within a shiny gold bag equals: {len(bags_unpacked)}")
    assert 20189 == len(bags_unpacked)

    return True
Example #2
0
def main():
    config = Config(day=9)

    # PART ONE

    # Test
    path_data = os.path.join(config.path_data, "xmas_test.txt")
    data_test = read_txt_file(path_file=path_data)
    data_test = [int(val) for val in data_test]
    encoding_error_test = detect_encoding_error(sequence=data_test, preamble=5)
    assert 127 == encoding_error_test

    path_data = os.path.join(config.path_data, "xmas.txt")
    data = read_txt_file(path_file=path_data)
    data = [int(val) for val in data]
    encoding_error = detect_encoding_error(sequence=data, preamble=25)
    assert 167829540 == encoding_error
    print(f"The first digit which does not abide to XMAX encoding is: {encoding_error}")

    # PART TWO

    # Test
    encoding_weakness_test = detect_encryption_weakness(sequence=data_test, encoding_error=encoding_error_test)
    assert 62 == encoding_weakness_test

    encoding_weakness = detect_encryption_weakness(sequence=data, encoding_error=encoding_error)
    print(
        f"The sum of the min and max of the contiguous sequence of numbers, "
        f"which sum equals the encoding error equals: {encoding_weakness}"
    )
    assert 28045630 == encoding_weakness

    return True
def main():
    config = Config(day=6)

    path_file = os.path.join(config.path_data, "custom_declaration_forms.txt")

    # PART ONE
    path_file_test = os.path.join(config.path_data,
                                  "custom_declaration_forms_test.txt")
    data_test = read_txt_file(path_file=path_file_test)
    data_test_group = split_group_data(data=data_test)

    sum_of_counts = compute_part_one(groups=data_test_group)
    assert 11 == sum_of_counts, f"Script does not lead to the correct sum. 11 != {sum_of_counts}"

    data = read_txt_file(path_file=path_file)
    groups = split_group_data(data=data)
    sum_of_counts = compute_part_one(groups=groups)
    print(
        f"The sum of the counts of the unique questions to which a group answered yes equals: {sum_of_counts}"
    )
    assert 6585 == sum_of_counts

    # PART TWO
    data_test_group = split_group_data(data=data_test)
    sum_of_counts = compute_part_two(groups=data_test_group)
    assert 6 == sum_of_counts, f"Script does not lead to the correct sum. 6 != {sum_of_counts}"

    groups = split_group_data(data=data)
    sum_of_counts = compute_part_two(groups=groups)
    print(
        f"The sum of the counts of the questions to which all group members answered yes: {sum_of_counts}"
    )

    return True
    pass
Example #4
0
def parse_ticket_info(path_file: str) -> Tuple[dict, list, list]:
    data = read_txt_file(path_file)
    notes = []
    ticket_mine = []
    tickets_nearby = []

    line_type = "note"
    for line in data:

        if not line:
            continue

        if not line.startswith("your ticket:") and line_type == "note":
            notes.append(line)
        elif line.startswith("your ticket"):
            line_type = "ticket_mine"
        elif line.startswith("nearby tickets:"):
            line_type = "tickets_nearby"
        elif line_type == "ticket_mine":
            ticket_mine.append(line)
        elif line_type == "tickets_nearby":
            tickets_nearby.append(line)
        else:
            raise ValueError

    notes = parse_notes(notes=notes)
    ticket_mine = parse_tickets(ticket_mine)[0]
    tickets_nearby = parse_tickets(tickets_nearby)

    return notes, ticket_mine, tickets_nearby
def read_adapter_data(path_file: str) -> np.ndarray:
    adapters = read_txt_file(path_file=path_file)
    adapters = np.array([int(val) for val in adapters])
    adapters = np.sort(adapters)
    adapters = np.insert(adapters, 0, 0)  # Add source joltage
    adapters = np.append(adapters, np.max(adapters) + 3)  # Add device joltage

    return adapters
Example #6
0
def parse_notes_two(path_file: str) -> (int, List[int]):
    notes = read_txt_file(path_file=path_file)
    assert len(notes) == 2

    timestamp = int(notes[0])
    buslines = [b for b in notes[1].split(",")]

    return timestamp, buslines
def parse_initial_state(path_file: str, dims: int = 3) -> np.ndarray:
    data = read_txt_file(path_file=path_file)
    data = [re.split("", line)[1:-1] for line in data]
    data = np.array(data)

    data_shape_new = [s for s in data.shape]
    data_shape_new = data_shape_new if len(data_shape_new) == dims else data_shape_new + [1] * (dims - len(data_shape_new))
    data = data.reshape(data_shape_new)
    data[data == "#"] = 1
    data[data == "."] = 0
    data = data.astype(int)
    data = expand_state(state=data)
    return data
def main():
    config = Config(day=8)

    # PART ONE
    path_file_test = os.path.join(config.path_data, "boot_code_test.txt")
    path_file = os.path.join(config.path_data, "boot_code.txt")

    # Test set
    data_test = read_txt_file(path_file=path_file_test)
    print(f"The test boot code has {len(data_test)} lines")
    infinite_loop, accumulator = execute_boot_code(boot_code=data_test)
    assert infinite_loop, f"Execution of bootcode did not enter a infinite loop"
    assert 5 == accumulator, f"Accumulator value is incorrect. 5 != {accumulator}"

    data = read_txt_file(path_file=path_file)
    print(f"The boot code has {len(data)} lines")
    infinite_loop, accumulator = execute_boot_code(boot_code=data)
    print(
        f"The boot code accumulator equals {accumulator} before it hits an infinite loop"
    )
    assert infinite_loop, f"Execution of bootcode did not enter a infinite loop"
    assert 1939 == accumulator, f"Accumulator value is incorrect. 1939 != {accumulator}"

    # PART TWO
    infinite_loop, accumulator, boot_code = fix_boot_code(boot_code=data_test)
    assert not infinite_loop
    assert 8 == accumulator, f"Accumulator value is incorrect. 8 != {accumulator}"

    infinite_loop, accumulator, boot_code = fix_boot_code(boot_code=data)
    assert not infinite_loop
    print(
        f"The fixed boot code accumulator equals {accumulator} after running until termination"
    )
    assert 2212 == accumulator, f"Accumulator value is incorrect. 2212 != {accumulator}"

    return True
Example #9
0
def parse_program(path_program: str) -> (str, list):
    program = read_txt_file(path_program)

    mask_lines = [
        i for i, line in enumerate(program) if line.startswith("mask")
    ]

    if len(mask_lines) == 1:
        return [program]

    tasks_list = [
        program[mask_lines[i]:mask_lines[i + 1]]
        for i in range(len(mask_lines) - 1)
    ] + [program[mask_lines[-1]:]]
    return tasks_list
def execute_homework(path_homework: str, solver: Callable) -> list:
    homework = read_txt_file(path_homework)

    homework_solutions = [*map(solver, homework)]
    return homework_solutions
def parse_seat_system(path_file: str) -> np.ndarray:
    seat_system = read_txt_file(path_file=path_file)
    return np.array([[val for val in row] for row in seat_system])
Example #12
0
def parse_puzzle_input(path_data: str) -> list:
    data = read_txt_file(path_data)
    return [[l[0], int(l[1:])] for l in data]