<jupyter_start><jupyter_text>Read the input data<jupyter_code>if samp == 1:
    filename = "/sample.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 = t[0].split(",")
t = [int(x) for x in t]
#print(t)<jupyter_output><empty_output><jupyter_text>Part one<jupyter_code>def countfuel(crabs, target):
    fuel = 0
    for c in crabs:
        fuel += abs(c - target)
    return fuel
def day(te):
    minfuel = countfuel(te, 0)
    for j in range(max(te)):
        fuel = countfuel(te, j)
        #print(j, fuel, minfuel)
        minfuel = min(countfuel(te, j), minfuel)
    return minfuel
#day(t)<jupyter_output><empty_output><jupyter_text>Part two<jupyter_code>def fuelsum(steps):
    fs = 0
    s = 1
    while s <= steps:
        fs += s
        s += 1
    return fs
def countfuel_p2(crabs, target):
    fuel = 0
    for c in crabs:
        fuel += fuelsum(abs(c - target))
    return fuel
def day2(te):
    minfuel = countfuel_p2(te, 0)
    oldmin = minfuel
    mincounter = 0
    for j in range(max(te)):
        fuel = countfuel_p2(te, j)

        minfuel = min(fuel, minfuel)
        
        if oldmin == minfuel:
            if (0):
                print(j, fuel, minfuel, mincounter)
            mincounter += 1
        else:
            oldmin = minfuel
            mincounter = 0
        # break when the minimum doesn't change in a while
        if mincounter > 10:
            break
        # print stuff for debugging
        if (0) and (samp == 0):
            if (j > 490) and (j % 10 == 0):
                print(j, fuel, minfuel, mincounter)
            elif (j % 100 == 0):
                print(j, fuel, minfuel, mincounter)
    return minfuel
#day2(t)<jupyter_output><empty_output><jupyter_text>Run the programs<jupyter_code>if 1:
    time0 = time.time()
    if part == 1:
        print("Part 1: ", day(t))
    elif part == 2:
        print("Part 2: ", day2(t))
    elif part == 3:
        #run both
        print("Part 1: ", day(t))
        print("Part 2: ", day2(t))
    tdif = time.time() - time0
    print("Elapsed time: {:.4f} s".format(tdif))<jupyter_output>Part 1:  355150
Part 2:  98368490
Elapsed time: 14.3747 s
{'fraction_non_alphanumeric': 0.13682864450127877, 'fraction_numerical': 0.02855924978687127, 'mean_word_length': 2.1845318860244234, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, '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': '387626', 'license': 'MIT', 'n_tokens_mistral': 915, 'n_tokens_neox': 832, 'n_words': 231, 'path': '7/notebook.ipynb', 'repo_name': 'aapalo/aoc2021', 'size': '2346'}