Exemplo n.º 1
0
def main(filepath: str) -> int:

    rows: List[str] = read_file_line_by_line(filepath=filepath)
    orig_rows: List[str] = read_file_line_by_line(filepath=filepath)
    changable_indexes: Iterable[int] = iter(
        [i for i in range(len(rows)) if rows[i].split()[0] in ["nop", "jmp"]]
    )
    indexes: List[int] = []; ind: int = 0; acc: int = 0

    while True:
        new_index, new_acc = action(row=rows[ind])
        ind += new_index

        # swap condition
        if ind in indexes:
            switched_index: int = next(changable_indexes)
            indexes = []; ind = 0; acc = 0
            rows = orig_rows
            rows = change(rs=rows, index_to_change=switched_index)
            # there's some random memory leak here
            orig_rows = read_file_line_by_line(filepath=filepath)  
            assert orig_rows == read_file_line_by_line(filepath=filepath)
            continue

        # terminated successfully
        elif ind >= len(rows):
            acc += new_acc
            break
        
        else:
            indexes.append(ind)
            acc += new_acc

    return acc
Exemplo n.º 2
0
def main(filepath: str) -> int:

    files: List[str] = read_file_line_by_line(filepath=filepath)
    rules: List[Rule] = [create_rule(line) for line in files]
    shiny_gold_rules: List[Rule] = [
        rule for rule in rules if find_shiny_gold_bag(rule)
    ]
    bags: Set[str] = {rule.colour for rule in shiny_gold_rules}

    for b in rules:
        for base_bag in bags:
            if base_bag in b.bags.keys():
                bags.add(b.colour)
                break

    for b in rules:
        for base_bag in bags:
            if base_bag in b.bags.keys():
                bags.add(b.colour)
                break

    for b in rules:
        for base_bag in bags:
            if base_bag in b.bags.keys():
                bags.add(b.colour)
                break

    for b in rules:
        for base_bag in bags:
            if base_bag in b.bags.keys():
                bags.add(b.colour)
                break

    return len(bags)
Exemplo n.º 3
0
def main(file: str = "advent_of_code/day1/input.txt") ->  Optional[Tuple[int, int, int]]:
    input_data: List[str] = read_file_line_by_line(filepath=file)
    input_integers: List[int] = [cast_to_int(x=row) for row in input_data]
    number_triples: List[List[int]] = make_triples(data=input_integers)

    for triple in number_triples:
        res: Optional[Tuple[int, int, int]] = check_sum_v2(x=triple[0], y=triple[1], z=triple[2])
        if res is not None:
            return(res)
Exemplo n.º 4
0
def main(file: str = "") -> int:
    f: List[str] = read_file_line_by_line(filepath=file)

    results: List[bool] = []

    for line in f:
        policy = make_policy(line=line)
        results.append(is_password_valid(p=policy))

    count: int = 0 if not results.count(True) else results.count(True)

    return count
Exemplo n.º 5
0
def main(file: str = "") -> int:

    input_file: List[str] = read_file_line_by_line(filepath=file)
    count: int = 0
    index: int = 0

    for row in input_file:
        res: int = loop(len(row), index=index, row=row)
        count += 1 if res else 0
        index += 3

    return count
Exemplo n.º 6
0
def main(file: str = "") -> int:

    input_file: List[str] = read_file_line_by_line(filepath=file)

    conditions: List[Tuple[int, int]] = [(1, 1), (1, 3), (1, 5), (1, 7),
                                         (2, 1)]
    counts: List[int] = []

    for down, right in conditions:
        counts.append(slope(rows=input_file, slide=right, line_skip=down))

    return reduce((lambda x, y: x * y), counts)
Exemplo n.º 7
0
def main(filepath: str) -> int:

    rows: List[str] = read_file_line_by_line(filepath=filepath)
    indexes: List[int] = []
    ind: int = 0
    acc: int = 0

    while True:
        new_index, new_acc = action(row=rows[ind])
        ind += new_index

        if ind in indexes:
            break

        indexes.append(ind)
        acc += new_acc

    return acc
Exemplo n.º 8
0
def main(filepath: str) -> int:
    lines: List[str] = read_file_line_by_line(filepath=filepath)
    seats: List[Seat] = [create_seat(line=line) for line in lines]
    seat_ids: List[int] = sorted([i.seat_id for i in seats])

    return get_missing_seat(seat_ids=seat_ids)
Exemplo n.º 9
0
def main(filepath: str) -> int:
    lines: List[str] = read_file_line_by_line(filepath=filepath)
    seats: List[Seat] = [create_seat(line=line) for line in lines]
    sorted_seat: Seat = sorted(seats, key=lambda s: s.seat_id, reverse=True)[0]
    return sorted_seat.seat_id
Exemplo n.º 10
0
 def test_read_file_line_by_line(self) -> None:
     self.assertEqual(
         read_file_line_by_line(filepath="tests/helpers/helpers_test.txt"),
         ["this", "is", "a", "test"])
Exemplo n.º 11
0
 def setUp(self) -> None:
     self.rows = read_file_line_by_line("tests/day3/input_test.txt")