Example #1
0
def calculate_starting_black_tiles():
    lines = read_file("input.txt")
    lines = [line for line in lines if line]
    black_tiles = set()

    for line in lines:
        position = (0, 0)
        i = 0
        while i < len(line):
            row, col = position
            if line[i] == "e":
                position = (row + 0, col + 2)
                i += 1
            elif line[i] == "w":
                position = (row + 0, col - 2)
                i += 1
            elif line[i:i + 2] == "se":
                position = (row + 1, col + 1)
                i += 2
            elif line[i:i + 2] == "sw":
                position = (row + 1, col - 1)
                i += 2
            elif line[i:i + 2] == "nw":
                position = (row - 1, col - 1)
                i += 2
            elif line[i:i + 2] == "ne":
                position = (row - 1, col + 1)
                i += 2

        if position in black_tiles:
            black_tiles.remove(position)
        else:
            black_tiles.add(position)

    return black_tiles
Example #2
0
def parse():
    lines = read_file("input.txt")

    fields = {}
    tickets = []

    iterator = iter(lines)
    while line := next(iterator):
        field, *ranges = re.match(
            "(.+): ([0-9]+)-([0-9]+) or ([0-9]+)-([0-9]+)", line).groups()
        ranges = list(map(int, ranges))
        fields[field] = [tuple(ranges[:2]), tuple(ranges[2:])]
Example #3
0
    current_grid = deepcopy(grid)
    changed = True
    while changed:
        changed = False
        new_grid = deepcopy(current_grid)

        for r in range(len(current_grid)):
            for c in range(len(current_grid[r])):
                seat = current_grid[r][c]
                n_neighbors = calculation_method(current_grid, r, c)
                if seat == EMPTY_SEAT and n_neighbors == 0:
                    new_grid[r][c] = OCCUPIED_SEAT
                    changed = True
                if seat == OCCUPIED_SEAT and n_neighbors >= min_neighbors:
                    new_grid[r][c] = EMPTY_SEAT
                    changed = True

        current_grid = new_grid
    return sum(seat == OCCUPIED_SEAT for row in current_grid for seat in row)


if __name__ == '__main__':
    lines = read_file("input.txt")
    grid = [[place for place in row] for row in lines if row]

    answer_part_1 = find_stable_occupied_seats(grid, neighbors_part_1, 4)
    print(f"Part 1: {answer_part_1}")

    answer_part_2 = find_stable_occupied_seats(grid, neighbors_part_2, 5)
    print(f"Part 2: {answer_part_2}")
Example #4
0
import fileinput
import re
from collections import deque
from copy import deepcopy

from read_file.read_file import read_file

D1 = deque()
D2 = deque()
active_deck = D1

L = read_file("input.txt")
for l in L:
    if 'Player' in l:
        if '2' in l:
            active_deck = D2
    elif l:
        active_deck.append(int(l))

t = 0
def play_game(D1, D2, is_p2):
    SEEN = set()
    while D1 and D2:
        global t
        t += 1
        my_key = (tuple(D1),tuple(D2))
        if my_key in SEEN and is_p2:
            return True,D1
        SEEN.add(my_key)
        c1,c2 = D1.popleft(), D2.popleft()
        if len(D1)>=c1 and len(D2)>=c2 and is_p2:
            number = int(expression[start_i:i])
            process_number(number)
        elif c in ["+", "*"]:
            assert current_op is None
            current_op = c
            i += 1
        elif c == "(":
            stack.append((current_value, current_op))
            current_value, current_op = 0, None
            i += 1
        elif c == ")":
            number = current_value
            current_value, current_op = stack.pop()
            process_number(number)
            i += 1
        else:
            assert c == " "
            i += 1

    return current_value


if __name__ == '__main__':
    import doctest
    doctest.testmod()

    expressions = read_file("input.txt")
    answer_part_1 = sum(
        evaluate(expression) for expression in expressions if expression)
    print(f"Part 1: {answer_part_1}")
Example #6
0
from read_file.read_file import read_file


def has_2_sum(number, numbers):
    for i, number_a in enumerate(numbers):
        for _, number_b in enumerate(numbers, i + 1):
            if number == number_a + number_b:
                return True
    return False


if __name__ == '__main__':
    sliding_window = 25
    numbers = [int(line) for line in read_file("input.txt") if line]

    i = sliding_window + 1
    while has_2_sum(numbers[i], numbers[i - sliding_window:i]):
        i += 1

    answer_part_1 = numbers[i]
    print(f"Part 1: {answer_part_1}")

    count = 0
    start_i_in_range = 0
    start_i_not_in_range = 0
    while count != answer_part_1:
        if count + numbers[start_i_not_in_range] <= answer_part_1:
            count += numbers[start_i_not_in_range]
            start_i_not_in_range += 1
        else:
            count -= numbers[start_i_in_range]
Example #7
0
from read_file.read_file import read_file


def calculate_seat_id(seat):
    return int(
        seat.replace("F", "0").replace("B",
                                       "1").replace("L",
                                                    "0").replace("R", "1"), 2)


if __name__ == '__main__':
    seats = read_file("input.txt")[:-1]
    part_1 = max(calculate_seat_id(seat) for seat in seats)
    print(f"Part 1: {part_1}")

    present_seat_ids = {calculate_seat_id(seat) for seat in seats}
    my_seat_id = [
        seat_id for seat_id in range(7112 + 1)
        if seat_id not in present_seat_ids and seat_id +
        1 in present_seat_ids and seat_id - 1 in present_seat_ids
    ][0]
    print(f"Part 2: {my_seat_id}")