Пример #1
0
def main():
    filename = f"./raw_inputs/day11{'_debug' if DEBUG else ''}.txt"
    # we could map these to e.g., True/False/None but having them as-is is easier for
    # visualizing amd comparing debug printouts to provided examples

    for part in (1, 2):
        seat_map = parser.parse(
            filename, transforms=[parser.Map({
                "#": "#",
                "L": "L",
                ".": "."
            })])

        map_min, map_max = _get_map_extents(seat_map)

        if DEBUG:
            print()
            for row in seat_map:
                print(''.join(row))

        while True:
            seat_map_working_copy = copy.deepcopy(seat_map)
            for i in range(map_min[0], map_max[0]):
                for j in range(map_min[1], map_max[1]):
                    if seat_map[i][j] != ".":
                        adjacent = _count_adjacent(
                            seat_map,
                            i,
                            j,
                            "#",
                            max_dist=1 if part == 1 else None)
                        if seat_map[i][j] == "#" and adjacent >= (4 if part
                                                                  == 1 else 5):
                            seat_map_working_copy[i][j] = "L"
                        elif seat_map[i][j] == "L" and adjacent == 0:
                            seat_map_working_copy[i][j] = "#"

            if DEBUG:
                print()
                for row in seat_map_working_copy:
                    print(''.join(row))

            if seat_map_working_copy == seat_map:
                break
            else:
                seat_map = copy.deepcopy(seat_map_working_copy)

        occupied = sum(row.count("#") for row in seat_map)
        print(f"part {part}: {occupied} seats occupied")

    print("fin.")
Пример #2
0
import AdventOfCode.AoC2020.input_parser as parser
import re
from collections import namedtuple

ValidationRule = namedtuple("ValidationRule", ["regex", "groups_validator"])

if __name__ == "__main__":
    data = parser.parse(
        "./raw_inputs/day4.txt",
        blob=True,
        transforms=[parser.Split("\n| "),
                    parser.Split("\n\n")])

    valid = 0
    invalid = 0

    year = re.compile('^(\d{4})$')

    # dict of field key: (regex for val, validation func to operate on .groups() from regex match)
    all_fields = {
        'byr':
        ValidationRule(year, lambda x: 1920 <= int(x[0]) <= 2002),
        'iyr':
        ValidationRule(year, lambda x: 2010 <= int(x[0]) <= 2020),
        'eyr':
        ValidationRule(year, lambda x: 2020 <= int(x[0]) <= 2030),
        'hgt':
        ValidationRule(
            re.compile('^(\d+)(cm|in)$'), lambda x: 150 <= int(x[0]) <= 193
            if x[1] == 'cm' else 59 <= int(x[0]) <= 76),
        'hcl':
Пример #3
0
import AdventOfCode.AoC2020.input_parser as parser
import re
from collections import defaultdict

PART = 2
DEBUG = False


def _debug(msg):
    if DEBUG:
        print(msg)


if __name__ == "__main__":
    filename = f"./raw_inputs/day8{'_debug' if DEBUG else ''}.txt"
    instructions = parser.parse(filename, transforms=[parser.Split(' ')])
    for instruction in instructions:
        instruction[1] = int(instruction[1])

    accumulator = 0
    instr_ptr = 0
    visited = set()
    nop_jmp_switch = 0
    nop_jmp_counter = 0

    while True:
        if instr_ptr in visited:
            print(f"Loop detected. Accum = {accumulator}")
            if PART == 1:
                break
            elif PART == 2:
Пример #4
0
import AdventOfCode.AoC2020.input_parser as parser
import re
from collections import defaultdict

DEBUG = False


def _debug(msg):
    if DEBUG:
        print(msg)


if __name__ == "__main__":
    filename = f"./raw_inputs/day9{'_debug' if DEBUG else ''}.txt"
    data = parser.parse(filename, transforms=[parser.Cast(int)])

    preamble_length = 25 if not DEBUG else 5

    exploit = -1
    match = False
    for i in range(preamble_length, len(data)):
        preamble = data[i - preamble_length:i]
        value = data[i]
        if DEBUG:
            print(preamble)

        # brute force it first..
        match = False
        for val1 in preamble:
            for val2 in preamble:
                if val1 == val2:
Пример #5
0
import AdventOfCode.AoC2020.input_parser as parser
import re
from collections import defaultdict

DEBUG = False


def _debug(msg):
    if DEBUG:
        print(msg)


if __name__ == "__main__":
    filename = f"./raw_inputs/day10{'_debug' if DEBUG else ''}.txt"
    adapters = parser.parse(filename, transforms=[parser.Cast(int)])

    adapters.sort()
    diffs = defaultdict(lambda: 0)
    diffs[adapters[0] - 0] += 1

    # initial diff already captured in list init
    for i in range(1, len(adapters)):
        diffs[adapters[i] - adapters[i - 1]] += 1

    diffs[3] += 1  # for the device built-in

    part1 = diffs[1] * diffs[3]
    print(part1)

    # part 2 - realizing now this was a graph problem, for the sake of neatness going to just rebuild from scratch
    # instead of trying to shoe-horn into the "build a list of diffs and count" approach of part 1
Пример #6
0
import AdventOfCode.AoC2020.input_parser as parser
import re
from collections import namedtuple

if __name__ == "__main__":
    data = parser.parse("./raw_inputs/day6.txt",
                        transforms=[parser.Split('\n'),
                                    parser.Split('\n\n')],
                        blob=True)

    group_answers = []
    group_unanimous = []
    for group in data:
        all_yes_answers = set()
        common_yes_answers = None
        for single_answerset in group:
            yes_answers = set(iter(single_answerset))
            all_yes_answers.update(yes_answers)
            if common_yes_answers is None:
                common_yes_answers = yes_answers
            else:
                common_yes_answers.intersection_update(yes_answers)
        group_answers.append(all_yes_answers)
        group_unanimous.append(common_yes_answers)

    total_yes = sum([len(group_answer) for group_answer in group_answers])
    print(f"{total_yes}")
    total_unanimous = sum(
        [len(group_answer) for group_answer in group_unanimous])
    print(f"{total_unanimous}")
Пример #7
0
import AdventOfCode.AoC2020.input_parser as parser
import re
from collections import namedtuple

ValidationRule = namedtuple("ValidationRule", ["regex", "groups_validator"])

if __name__ == "__main__":
    data = parser.parse("./raw_inputs/day5.txt")

    max_seat_id = 0
    seat_ids = []

    for line in data:
        row_code = line[0:7].replace("F", "0").replace("B", "1")
        seat_code = line[-3:].replace("L", "0").replace("R", "1")

        row = int(row_code, 2)
        seat = int(seat_code, 2)
        seat_id = row * 8 + seat
        seat_ids.append(seat_id)
        print(seat_id)

    print(f"MAX seat_id: {max(seat_ids)}")

    seat_ids.sort()
    my_seat = None
    for i in range(1, len(seat_ids)):
        if seat_ids[i - 1] + 1 != seat_ids[i]:
            my_seat = seat_ids[i - 1] + 1
            continue