<jupyter_start><jupyter_text>Read the input data<jupyter_code>#time0 = time.time()

if samp == 1:
    filename = "/sample.txt"
elif samp == 3:
    filename = "/input13.txt"
else:
    filename = "/input.txt"
try:
    with open(str(date) + filename,"r") as f:
        t = f.readlines()
except FileNotFoundError:
    with open("." + filename,"r") as f:
        t = f.readlines()

t = [(x.strip().replace('  ',' ')) for x in t]
#t = [int(x) for x in t]<jupyter_output><empty_output><jupyter_text>Part one<jupyter_code>def foldpaper_x(paper, pos):
    newpaper = []
    for r in range(len(paper)):
        newpaper.append([0] * pos)
        for c in range(pos):
            newpaper[r][c] = paper[r][c] or paper[r][-(1+c)]
            
    return newpaper
def foldpaper_y(paper, pos):
    newpaper = []
    for r in range(pos):
        newpaper.append(paper[r])
        for c in range(len(paper[0])):
            try:
                newpaper[r][c] |= paper[pos*2-r][c]
            except:
                #print("ERROR: pos {}, pos*2-r {}, len {}".format(pos, pos*2-r, len(paper)))
                pass
    return newpaper
def printpaper(paper):
    # print dots
    for row in paper:
        r = ""
        for c in row:
            if c > 0:
                r += "#"
            else:
                r += " "
        print(r)
    return 1
def day(te, p):
    folds = []
    paper = []
    coords = set()
    x = []
    y = []
    for row in te:
        try:
            if "fold" not in row:
                row = row.split(",")
                x.append(int(row[0]))
                y.append(int(row[1]))
            else:
                row = row.split("=")
                axis = row[0].split(" ")[-1]
                fol = int(row[1])
                folds.append((axis,fol))
        except:
            continue
    
    # create the empty paper
    #for row in range(max(y)+1):
        #paper.append([0] * (max(x)+1))
    paper = [[0] * (max(x)+1) for i in range(max(y)+1)]
    
    # add the dots
    for i in range(len(y)):
        paper[y[i]][x[i]] = 1
    
    # fold the paper
    alldots = []
    for (axis, location) in folds:            
        if axis == "x":
            paper = (foldpaper_x(paper,location))
        elif axis == "y":
            paper = (foldpaper_y(paper,location))
        # calculate dots
        dots = 0
        for row in paper:
            dots += sum(row)
            if 0:
                print(row)
        if p == 1:
            return dots
        alldots.append(dots)
    #print(alldots)
    printpaper(paper)
    return dots
day(t,2)<jupyter_output>#### ###  ####  ##  #  #  ##  #  # #  # 
#    #  #    # #  # # #  #  # #  # #  # 
###  #  #   #  #    ##   #    #### #  # 
#    ###   #   # ## # #  #    #  # #  # 
#    #    #    #  # # #  #  # #  # #  # 
#### #    ####  ### #  #  ##  #  #  ##<jupyter_text>Part two<jupyter_code>def day2(te):
    return day(t,2)
day2(t)<jupyter_output>#### ###  ####  ##  #  #  ##  #  # #  # 
#    #  #    # #  # # #  #  # #  # #  # 
###  #  #   #  #    ##   #    #### #  # 
#    ###   #   # ## # #  #    #  # #  # 
#    #    #    #  # # #  #  # #  # #  # 
#### #    ####  ### #  #  ##  #  #  ##<jupyter_text>Run the programs<jupyter_code>if 1:
    time0 = time.time()
    if part == 1:
        print("Part 1: ", day(t,1))
    elif part == 2:
        print("Part 2: ", day2(t))
    elif part == 3:
        #run both
        print("Part 1: ", day(t,1))
        print("Part 2: ", day2(t))
    tdif = time.time() - time0
    print("Elapsed time: {:.4f} s".format(tdif))<jupyter_output>Part 1:  837
#### ###  ####  ##  #  #  ##  #  # #  # 
#    #  #    # #  # # #  #  # #  # #  # 
###  #  #   #  #    ##   #    #### #  # 
#    ###   #   # ## # #  #    #  # #  # 
#    #    #    #  # # #  #  # #  # #  # 
#### #    ####  ### #  #  ##  #  #  ##  
Part 2:  99
Elapsed time: 0.3687 s
{'fraction_non_alphanumeric': 0.2033195020746888, 'fraction_numerical': 0.015300829875518672, 'mean_word_length': 1.456687898089172, 'pattern_counts': {'":': 2, '<': 14, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '258929', 'license': 'MIT', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1474, 'n_words': 270, 'path': '13/notebook.ipynb', 'repo_name': 'aapalo/aoc2021', 'size': '3856'}