<jupyter_start><jupyter_text>Day 01 Problem 1count the number of times a depth measurement increases from the previous measurement.(There is no measurement before the first measurement.)  Example199 200 208 210 200 207 240 269 260 263 199 (N/A - no previous measurement)**200 (increased)** **208 (increased)** **210 (increased)** 200 (decreased) **207 (increased)** **240 (increased)** **269 (increased)** 260 (decreased) **263 (increased)** So the answer is 7.  Steps to take1. Define a list variable and assign the numbers.2. Measure the length of the list.3. Repeat comparing i-th element and i+1-th element from i = 0 to the end.4. Define a count variable and set it to 0.5. If i+1-th element is larger than i-th element, then add 1 to the variable defined at step 4.6. Make it into a function.  Step 1: Define a list variable and assign the numbers<jupyter_code>with open('example.txt','r') as input_text:
    numbers = input_text.read()

print(numbers)
type(numbers)<jupyter_output>199
200
208
210
200
207
240
269
260
263<jupyter_text>As it is, the variable becomes string. this needs to be converted to a list.<jupyter_code>numbers_list = numbers.split('\n')
print(numbers_list)<jupyter_output>['199', '200', '208', '210', '200', '207', '240', '269', '260', '263']<jupyter_text>Oh, no, this is a list of strings! they need to be converted to numeric values.<jupyter_code>num_list = [int(n) for n in numbers_list]
print(num_list)<jupyter_output>[199, 200, 208, 210, 200, 207, 240, 269, 260, 263]<jupyter_text>Step 2: Measure the length of the list.<jupyter_code>length_list = len(num_list)
print(length_list)<jupyter_output>10<jupyter_text>Step 3: Repeat comparing i-th element and i+1-th element from i = 0 to the end<jupyter_code>for i in range (1,length_list):
    print(num_list[i])<jupyter_output>200
208
210
200
207
240
269
260
263<jupyter_text>**Lessons**1. for loop needs ":" in the end2. if not "in []" but incremental numbers in range, then use "in range (start, last)<jupyter_code>for i in range (1,length_list):
    if num_list[i] > num_list[i-1]:
        print("increased")
    else:
        print("decreased")<jupyter_output>increased
increased
increased
decreased
increased
increased
increased
decreased
increased<jupyter_text>**Lessons**3. it is not "not if", but "else"  Step 4: Define a count variable and set it to 0.<jupyter_code>count = 0
print(count)<jupyter_output>0<jupyter_text>Step 5: If i+1-th element is larger than i-th element, then add 1 to the variable defined at step 4.<jupyter_code>for i in range (1,length_list):
    if num_list[i] > num_list[i-1]:
        count = count+1
        print(count)<jupyter_output>1
2
3
4
5
6
7<jupyter_text>Step 6: Make it into a function<jupyter_code>def count_depth_increase(input_file):
    with open(input_file,'r') as input_text:
        numbers_str = input_text.read()
    numbers_str_list = numbers_str.split('\n')
    num_list = [int(n) for n in numbers_str_list]
    length_list = len(num_list)
    count = 0
    for i in range (1,length_list):
        if num_list[i] > num_list[i-1]:
            count = count+1
    print(count)        
count_depth_increase("example.txt")<jupyter_output>7<jupyter_text>Oh, it seems it functions, yay!<jupyter_code>count_depth_increase("depth.txt")<jupyter_output>1387<jupyter_text>Yay, I got the right answer!!!  Problem 2Count the number of times the sum of 3 measurements in this sliding window increases from the previous sum. ExampleIn the case of the example, it will be:```A: 607 (N/A - no previous sum)B: 618 (increased)C: 618 (no change)D: 617 (decreased)E: 647 (increased)F: 716 (increased)G: 769 (increased)H: 792 (increased)````and the answer is 5. Steps to takeDefine a new empty list where the sum of 3 measurements can be stored.Otherwise apply what I made for Problem 1.<jupyter_code>window_3_measurements = [num_list[i]+num_list[i+1]+num_list[i+2] for i in range (0, length_list-2)]
print(window_3_measurements)<jupyter_output>[607, 618, 618, 617, 647, 716, 769, 792]<jupyter_text>**Lessons**4. in this case, the range is to be length_list-2 but not -3<jupyter_code>count_window_increase = 0
# see how index is used below..
print(window_3_measurements[0])
print(length_list)
print(window_3_measurements[length_list-2-1])
for i in range (0, length_list-2-1):
    if window_3_measurements[i] < window_3_measurements[i+1]:
        count_window_increase = count_window_increase + 1
print(count_window_increase)<jupyter_output>5<jupyter_text>Define a function. Probabaly it can be modularized? Steps to take to modularize codes1. define a function to make a list of numeric values from a text file2. define a function to make a list of windows based on the input list3. define a function to compare list elements within the input list and count how many times an element in the list is larger than the previous element Step 1: Define a function to make a list of numeric values from a text file<jupyter_code>def make_num_list(input_file):
    with open(input_file,'r') as input_text:
        numbers_str = input_text.read()
    numbers_str_list = numbers_str.split('\n')
    num_list = [int(n) for n in numbers_str_list]
    return(num_list)
make_num_list("example.txt")<jupyter_output><empty_output><jupyter_text>Step 2: Define a function to make a list of windows based on the input listIt should be probably better to be able to define the size of window.<jupyter_code>win_width = 3
window_3_measurements_test = []
sum_3 = 0
for i in range (0, length_list-(win_width-1)):
    for j in range (i, i+(win_width)):
        sum_3 += num_list[j]
    window_3_measurements_test.append(sum_3)
    sum_3 = 0
print(window_3_measurements_test)
type(window_3_measurements_test)
def make_win_list(input_num_list, win_width):
    length_list = len(input_num_list)
    window_sum_measurements = []
    sum_win = 0
    for i in range (0, length_list-(win_width-1)):
        for j in range (i, i+(win_width)):
        sum_win += num_list[j]
        window_sum_measurements.append(sum_win)
        sum_win = 0
    return(window_sum_measurements)  
num_list = make_num_list("example.txt")
print(type(num_list))
win_list = make_win_list(num_list,3)
print(type(win_list))<jupyter_output><class 'list'>
<class 'list'><jupyter_text>Step 3: Define a function to compare list elements within the input list and count how many times an element in the list is larger than the previous element<jupyter_code>def count_win_depth_increase(input_win_list):
    length_list = len(input_win_list)
    count = 0
    for i in range (1,length_list):
        if input_win_list[i] > input_win_list[i-1]:
            count = count+1
    print(count)      
count_win_depth_increase(win_list)<jupyter_output>5<jupyter_text>OK, it seems it functions finally. Let's try with the depth.txt.<jupyter_code>num_list = make_num_list("depth.txt")
win_list = make_win_list(num_list,3)
count_win_depth_increase(win_list)<jupyter_output>1362<jupyter_text>Hurray, I got the right answer!!! Congratulations on the first achievement :D  RevisionAccording to [this page](https://github.com/TechnoTone/AdventOfCode2021/blob/main/Python/day_01.py), it seems it is possible to add list items by setting range "[x:y]". So let's try this.<jupyter_code>num_list_ex = make_num_list("example.txt")
print(num_list_ex)
win_list_ex = [sum(num_list_ex[i:i+3]) for i in range (len(num_list_ex)-2)]
print(win_list_ex)<jupyter_output>[607, 618, 618, 617, 647, 716, 769, 792]<jupyter_text>OK, it seems it works. Let's revise `make_win_list` function.<jupyter_code>def make_win_list_2(input_num_list, win_width):
    window_sum_measurements = [sum(input_num_list[i:i+win_width]) for i in range (len(input_num_list)-(win_width-1))]
    return(window_sum_measurements)  
num_list = make_num_list("depth.txt")
win_list = make_win_list_2(num_list,3)
count_win_depth_increase(win_list)<jupyter_output>1362
{'fraction_non_alphanumeric': 0.10350095480585614, 'fraction_numerical': 0.04888605983450032, 'mean_word_length': 5.210276679841897, 'pattern_counts': {'":': 1, '<': 59, '<?xml version=': 0, '>': 62, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '429770', 'license': 'MIT', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2719, 'n_words': 823, 'path': 'day_01/learning_note.ipynb', 'repo_name': 'naoe-tatara/aoc_2021', 'size': '7855'}