<jupyter_start><jupyter_text>Day 1  Part 1<jupyter_code>with open('input-01.txt', 'r') as file:
    lines = list(map(lambda x: int(x), file.readlines()))
def solve_1(measurements):
    increases = 0
    for i in range(1, len(measurements)):
        if measurements[i] > measurements[i-1]:
            increases += 1
    return increases
solve_1(lines)<jupyter_output><empty_output><jupyter_text>Part 2<jupyter_code>def solve_2(measurements):
    measurements_2 = []
    for i in range(len(measurements)-2):
        measurements_2.append(sum(measurements[i:i+3]))
    return solve_1(measurements_2)
solve_2(lines)
import matplotlib.pyplot as plt
with open('input-07.txt', 'r') as file:
    pos = sorted(list(map(lambda x: int(x), file.readline().split(','))))
def fuel_cost(x1, x2):
    d = abs(x1 - x2)
    return d * (d+1) / 2
def solve(positions):
    fuel_costs = []
    for i in range(max(positions)):
        fuel = 0
        for p in positions:
            fuel += fuel_cost(p, i)
        fuel_costs.append(fuel)
#     plot.plot(fuel_costs)
#     plt.plot(fuel_costs[400:600])
    return fuel_costs.index(min(fuel_costs))
solve(pos)
def weighted_average(numbers):
    num_dict = {}
    for n in numbers:
        num_dict[n] = num_dict.get(n, 0) + 1
    weighted_sum = 0
    for n in num_dict:
        weighted_sum += n * num_dict[n]
    return weighted_sum/len(numbers)
weighted_average(pos)
len(pos)<jupyter_output><empty_output><jupyter_text>Day 7 Rough<jupyter_code>with open('input-08.txt', 'r') as file:
    signals = file.readlines()
segment_digit_map = {
    ('a', 'b', 'c', 'e', 'f', 'g'): 0,
    ('c', 'f'): 1,
    ('a', 'c', 'd', 'e', 'g'): 2,
    ('a', 'c', 'd', 'f', 'g'): 3,
    ('b', 'c', 'd', 'f'): 4,
    ('a', 'b', 'd', 'f', 'g'): 5,
    ('a', 'b', 'd', 'e', 'f', 'g'): 6,
    ('a', 'c', 'f'): 7,
    ('a', 'b', 'c', 'd', 'e', 'f', 'g'): 8,
    ('a', 'b', 'c', 'd', 'f', 'g'): 9,
}
def get_mapping_from_digits(digits):
    digits = sorted(digits.split(), key=lambda x: len(x))
    # The segment x can be represented by the following
    candidates = {x: set() for x in 'abcdefg'}
    for d in digits:
        if len(d) == 2:
            candidates['c'] = set(d)
            candidates['f'] = set(d)
        elif len(d) == 3:
            candidates['a'] = set(d) - candidates['c']
        elif len(d) == 4:
            candidates['d'] = set(d) - candidates['c'] - candidates['a']
            candidates['b'] = set(d) - candidates['c'] - candidates['a']
        elif len(d) == 5:
            diff_set = set(d) - candidates['a'] - candidates['c']
            if len(diff_set) > 0:
                candidates['e'].update(diff_set)
                candidates['g'].update(diff_set)
    
    _ = set()
    _.update(set(digits[-1]) - set(digits[-2]))
    _.update(set(digits[-1]) - set(digits[-3]))
    _.update(set(digits[-1]) - set(digits[-4]))

    candidates['c'] = candidates['c'].intersection(_)
    candidates['d'] = candidates['d'].intersection(_)
    candidates['e'] = candidates['e'].intersection(_)
    
    while True:
        dirty = False
        for char in 'abcdefg':
            if len(candidates[char]) == 1:
                for key in candidates:
                    if key == char:
                        continue
                    else:
                        candidates[key] = candidates[key] - candidates[char]
            else:
                dirty = True
        if not dirty:
            break
    
    # x can represent the following segments
    possibilities = {x: set() for x in 'abcdefg'}
    for key in candidates:
        for letter in candidates[key]:
            possibilities[letter].add(key)
    return possibilities
p = get_mapping_from_digits('bgda cdg dacbe cfega begcafd febacd degcba adecg edcfbg gd')
print(p)
get_number_from_segments('febacd', p)
def get_digit_from_segments(segments, mapping):
    lit_segment_set = set()
    for s in segments:
        lit_segment_set.update(mapping[s])
    return str(segment_digit_map[tuple(sorted(tuple(lit_segment_set)))])
def get_number_from_segments(segments, mapping):
    n = ''
    for s in segments.split():
        n += get_digit_from_segments(s, mapping)
    return int(n)
def solve_2(numbers):
    total = 0
    for i in numbers:
        _ = i.split(' | ')
        digits = _[0]
        number = _[1]
        mapping = get_mapping_from_digits(digits)
        total += get_number_from_segments(number, mapping)
    return total
solve_2(signals)<jupyter_output>Set candidates of c and f to {'d', 'a'}
Set candidates of c and f to {'c', 'g'}
Set candidates of c and f to {'f', 'a'}
Set candidates of c and f to {'c', 'g'}
Set candidates of c and f to {'d', 'g'}
Set candidates of c and f to {'b', 'c'}
Set candidates of c and f to {'c', 'd'}
Set candidates of c and f to {'b', 'f'}
Set candidates of c and f to {'e', 'b'}
Set candidates of c and f to {'d', 'g'}
Set candidates of c and f to {'f', 'a'}
Set candidates of c and f to {'b', 'c'}
Set candidates of c and f to {'f', 'g'}
Set candidates of c and f to {'b', 'g'}
Set candidates of c and f to {'b', 'g'}
Set candidates of c and f to {'a', 'g'}
Set candidates of c and f to {'f', 'g'}
Set candidates of c and f to {'c', 'a'}
Set candidates of c and f to {'e', 'g'}
Set candidates of c and f to {'d', 'g'}
Set candidates of c and f to {'b', 'd'}
Set candidates of c and f to {'f', 'a'}
Set candidates of c and f to {'a', 'g'}
Set candidates of c and f to {'d', 'f'}
Set candidates of c and f to {'e', 'g'}
[...]
{'fraction_non_alphanumeric': 0.16818845872899926, 'fraction_numerical': 0.012235208181154127, 'mean_word_length': 2.769442532690984, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '117761', 'license': 'MIT', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1932, 'n_words': 593, 'path': '2021/2021 Attempts.ipynb', 'repo_name': 'hrushikeshrv/aoc', 'size': '5476'}