Beispiel #1
0
def run_part_1(in_file: str, debug: bool = False) -> int:
    pretty.printHeader(DAY, 1, inspect.stack()[0].function, in_file)
    result = 0
    treemap = loadingUtils.importTo2DArray(in_file)
    if debug: pretty.print2DMap(treemap)
    path = get_path_traverse_diagonal(treemap, (1, 3))
    result = len(list(filter(lambda x: x == "#", path)))
    print("I hit {} trees!".format(result))
    return result
Beispiel #2
0
def run_until_stable2(_seatmap, max_iter=100):
    seatmap = copy.deepcopy(_seatmap)

    for i in range(max_iter):
        print(i)
        old_seats = copy.deepcopy(seatmap)
        seatmap = copy.deepcopy(step2(seatmap))
        if old_seats == seatmap:
            pretty.print2DMap(seatmap)
            return sum(x.count("#") for x in seatmap)
    print("Did not converge")
    return 0
Beispiel #3
0
def match_columns(rules, clean_nearby):
    matches = []
    for id, column in enumerate(list(zip(*clean_nearby))):
        print("Column {:2} matches Rules:".format(id))
        match = []
        for rule_id, rule in enumerate(rules):
            if matches_rule(column, rule):
                print("  -- Rule {:2}".format(rule_id))
                match.append("1")
            else:
                match.append("0")
        matches.append(deepcopy(match))
    pretty.print2DMap(matches)
    pretty.print2DMap(zip(*matches))
Beispiel #4
0
def run_part_1(in_file: str, debug: bool = False) -> int:
    pretty.printHeader(DAY, 1, inspect.stack()[0].function, in_file)
    result = 0
    passports = loadingUtils.import_multiline(in_file)
    if debug: pretty.print2DMap(passports)
    count = 0
    for passport in passports:
        if complete_passport(passport):
            count += 1

    # code here
    result = count
    print("Result = {}".format(result))
    return result
Beispiel #5
0
def remove_monsters(image):
    monster_str = ""
    monster_str += "                  # \n"
    monster_str += "#    ##    ##    ###\n"
    monster_str += " #  #  #  #  #  #   "
    print(monster_str)
    monster_arr = [list(x) for x in monster_str.split("\n")]
    pretty.print2DArray(monster_arr)
    monster2 = []
    for x, l in enumerate(monster_arr):
        for y, v in enumerate(l):
            if v == "#":
                monster2.append((x, y))
    print(monster2)
    monster = []
    for idx, v in np.ndenumerate(monster_arr):
        if v == "#":
            monster.append(idx)
    print(monster)
    # for all rotations and flips
    for image in fliprot(image):
        print("Flip rot")
        pretty.print2DMap(image)
        monster_cnt = 0
        for idx, value in np.ndenumerate(image):
            #print(idx, value)
            matches_monster = True
            for m_idx in monster:
                try:
                    if image[tuple(np.add(idx, m_idx))] != "#":
                        matches_monster = False
                        break
                except IndexError:
                    matches_monster = False
                    break
            if matches_monster:
                print("found a monster starting at {}!".format(idx))
                monster_cnt += 1
                # replace # with O
                for m_idx in monster:
                    image[tuple(np.add(idx, m_idx))] = "O"
        print("Found {:2} monsters in this flip-rot".format(monster_cnt))
        if monster_cnt > 0:
            pretty.print2DMap(image)
            result = 0
            for idx, value in np.ndenumerate(image):
                if value == "#":
                    result += 1
            return result
Beispiel #6
0
def run_part_2(in_file: str, debug: bool = False) -> int:
    pretty.printHeader(DAY, 2, inspect.stack()[0].function, in_file)
    result = 0
    treemap = loadingUtils.importTo2DArray(in_file)
    if debug: pretty.print2DMap(treemap)
    list_of_directions = [(1, 1), (1, 3), (1, 5), (1, 7), (2, 1)]
    number_of_trees_per_run = []
    for direction in list_of_directions:
        number_of_trees_per_run.append(
            len(
                list(
                    filter(lambda x: x == "#",
                           get_path_traverse_diagonal(treemap, direction)))))
    print(number_of_trees_per_run)
    result = functools.reduce(lambda x, y: x * y, number_of_trees_per_run)
    print("Result = {}".format(result))
    return result
Beispiel #7
0
def assemble_image(tiles):
    stack = list(range(len(tiles)))
    fixed = []
    pos = {}  # array of positions
    x = 0
    y = 0
    print(stack)
    # get a starting Point (i.e. a edge)
    start = -1
    for i, tile in enumerate(tiles):
        if tile.is_edge:
            start = i
            break
    print("pick id = {} as start".format(start))
    stack.remove(start)
    fixed.append(start)
    pos[(x, y)] = start
    # rotate start so that left and up are unique
    start_tile = tiles[start]
    start_tile.flip()  # so we can compare to the example
    _i = 0
    while not (start_tile.upper() in start_tile.unique_sides
               and start_tile.left() in start_tile.unique_sides):
        print("rot90")
        start_tile.rot90()
        _i += 1
        if _i % 5 == 0:
            print("flip")
            start_tile.flip()
        print("{} {} =?= {}".format(start_tile.upper(), start_tile.left(),
                                    start_tile.unique_sides))
    start_tile.print()
    # find right neighbours until we reach the edge
    current_tile_id = start
    current_tile = start_tile
    while True:
        print(stack)
        print(pos)
        for next_id in current_tile.neighbours:
            if next_id in fixed:
                # skip neighbours that are already placed
                continue
            print("Try to place neighbour {}".format(next_id))
            next_tile = tiles[next_id]
            generator = next_tile.rotflip()
            while True:
                generator.__next__()
                if next_tile.left() == current_tile.right():
                    print("Found right neighbour")
                    pos[(x + 1, y)] = next_id
                    stack.remove(next_id)
                    fixed.append(next_id)
                    break
                if next_tile.upper() == current_tile.lower():
                    print("Found lower neighbour")
                    pos[(x, y + 1)] = next_id
                    stack.remove(next_id)
                    fixed.append(next_id)
                    break
        x = x + 1
        if current_tile.right() in current_tile.unique_sides:
            y += 1
            x = 0
            if current_tile.lower() in current_tile.unique_sides:
                assert len(stack) == 0
                break
        current_tile_id = pos[(x, y)]
        current_tile = tiles[current_tile_id]
    print(stack)
    print(pos)
    max_x, max_y = get_max(pos)
    print(max_x, max_y)
    print()
    a = []
    for y in range(max_y + 1):
        line_a = []
        for x in range(max_x + 1):
            print("{}".format(tiles[pos[(x, y)]].id), end=' ')
            line_a.append(tiles[pos[(x, y)]].get_inner())
        line = np.concatenate(line_a, axis=1)
        a.append(line)
        print()
        #pretty.print2DMap(line)
    full_map = np.concatenate(a, axis=0)
    pretty.print2DMap(full_map)
    return deepcopy(full_map)