コード例 #1
0
#!/usr/bin/env python3
"""
Solution to part 2
"""
from pathlib import Path

from aoc_io.aoc_io import DATA_FILENAME, submit_output

CURRENT_DIR = Path(__file__).resolve().parent
INPUT_FILE_PATH = CURRENT_DIR / DATA_FILENAME

mapping = str.maketrans({"B": "1", "F": "0", "R": "1", "L": "0"})

# Read input
with INPUT_FILE_PATH.open("r") as input_fp:
    seat_ids = {
        int(row_stripped.translate(mapping), 2)
        for row in input_fp.readlines() if (row_stripped := row.strip())
    }

result = 0
for seat_id in seat_ids:
    if seat_id + 1 not in seat_ids and seat_id + 2 in seat_ids:
        result = seat_id + 1

print(result)
print(submit_output(2020, 5, 2, result))
コード例 #2
0
#!/usr/bin/env python3
"""
Solution to part 2
"""
# pyright: reportUnknownMemberType=false
# pyright: reportUnknownVariableType=false
# pyright: reportUnknownArgumentType=false
from pathlib import Path

from aoc_io.aoc_io import DATA_FILENAME, submit_output

CURRENT_DIR = Path(__file__).resolve().parent
INPUT_FILE_PATH = CURRENT_DIR / DATA_FILENAME

# Read input
with INPUT_FILE_PATH.open("r") as input_fp:
    groups = [group for g in input_fp.read().split("\n\n") if (group := g.strip())]

count = 0
for group in groups:
    people = group.split()
    responses = set.intersection(*[set(person) for person in people])
    count += len(responses)

print(count)
print(submit_output(2020, 6, 2, count))
コード例 #3
0
        (str): Solution to the problem
    """
    cups: list[int] = read_input(_INPUT_FILE_PATH)
    max_ = max(cups)
    for _ in range(100):
        # Current element
        current = cups[0]
        # Elements not touched this loop
        left = cups[4:]
        left_set = set(left)
        # Find the next smallest value in the numbers left; wrap around to
        #   `max_` if the value decreases to 0
        while True:
            current -= 1
            if not current:
                current = max_
            if current in left_set:
                break
        # Add the 3 cups after the destination value, and then move the current
        #   value to the end of the list
        dest_index = cups.index(current) + 1
        cups = cups[4:dest_index] + cups[1:4] + cups[dest_index:] + cups[:1]
    index_1 = cups.index(1)
    return "".join(str(n) for n in (cups[index_1 + 1:] + cups[:index_1]))


if __name__ == "__main__":
    result = level1()
    print(result)
    print(submit_output(2020, 23, 1, result))
コード例 #4
0
with INPUT_FILE_PATH.open("r") as input_fp:
    map_: defaultdict[str, set[str]] = defaultdict(set)
    for line in input_fp:
        line = line.strip()
        if not line:
            continue
        root_color, children = line.split(" bags contain ")
        if children == "no other bags.":
            continue
        for child in children[:-1].split(", "):
            color_match = CHILDREN_COLOR_REGEX.fullmatch(child)
            if color_match is None:
                raise ValueError(child)
            color = color_match["color"]
            map_[color].add(root_color)

stack = ["shiny gold"]
available_colors = set()

while stack:
    color = stack.pop()
    outer_colors = map_[color]
    new_colors = outer_colors - available_colors
    available_colors |= outer_colors
    stack += list(new_colors)

count = len(available_colors)

print(count)
print(submit_output(2020, 7, 1, count))
コード例 #5
0
"""
from collections import deque
from pathlib import Path

from aoc_io.aoc_io import DATA_FILENAME, submit_output

CURRENT_DIR = Path(__file__).resolve().parent
INPUT_FILE_PATH = CURRENT_DIR / DATA_FILENAME

# Read input
with INPUT_FILE_PATH.open("r") as input_fp:
    nums = [int(line.strip()) for line in input_fp]

queue = deque(nums[:25])
left = deque(nums[25:])

while True:
    queue_set = set(queue)
    next_num = left.popleft()
    for num in queue:
        remainder = next_num - num
        if remainder in queue_set - {num}:
            break
    else:
        break
    queue.popleft()
    queue.append(next_num)

print(next_num)
print(submit_output(2020, 9, 1, next_num))
コード例 #6
0
        line = line.strip()
        if not line:
            continue
        root_color, children = line.split(" bags contain ")
        if children == "no other bags.":
            continue
        for child in children[:-1].split(", "):
            color_match = CHILDREN_COLOR_REGEX.fullmatch(child)
            if color_match is None:
                raise ValueError(child)
            count = int(color_match["count"])
            color = color_match["color"]
            map_[root_color][color] = count

stack: defaultdict[str, int] = defaultdict(lambda: 0)
stack["shiny gold"] = 1
total_count = 0

while stack:
    parent_color = next(iter(stack))
    parent_count = stack[parent_color]
    del stack[parent_color]
    rules = map_[parent_color]
    for child_color, child_count in rules.items():
        mul_child_count = parent_count * child_count
        total_count += mul_child_count
        stack[child_color] += mul_child_count

print(total_count)
print(submit_output(2020, 7, 2, total_count))
コード例 #7
0
INPUT_FILE_PATH = CURRENT_DIR / DATA_FILENAME

# Read input
with INPUT_FILE_PATH.open("r") as input_fp:
    code = []
    for line in input_fp:
        command, param = line.strip().split()
        code.append((command, int(param)))

ids = set()
acc = 0
pointer = 0

while True:
    if pointer in ids:
        break
    ids.add(pointer)
    command, param = code[pointer]
    if command == "acc":
        acc += param
        pointer += 1
    elif command == "jmp":
        pointer += param
    elif command == "nop":
        pointer += 1
    else:
        raise ValueError(f"{acc} {pointer}")

print(acc)
print(submit_output(2020, 8, 1, acc))
コード例 #8
0
from aoc_io.aoc_io import DATA_FILENAME, submit_output

CURRENT_DIR = Path(__file__).resolve().parent
INPUT_FILE_PATH = CURRENT_DIR / DATA_FILENAME

# Read input
with INPUT_FILE_PATH.open("r") as input_fp:
    nums = [int(line.strip()) for line in input_fp]

target = 373803594


def main() -> int:
    for i, num_start in enumerate(nums):
        sum_ = num_start
        for j, num in enumerate(nums[i + 1:]):
            sum_ += num
            if sum_ > target:
                break
            if sum_ == target:
                arr = nums[i:i + j + 2]
                return min(arr) + max(arr)
    return 0  # Make linter happy


sum_ = main()

print(sum_)
print(submit_output(2020, 9, 2, sum_))