File size: 1,706 Bytes
0f06ae9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy as np 


with open("/home/yassinealouini/Documents/code/advent_of_code/aoc/year_2021/data/1.txt") as f:
    data = f.read().rstrip().split("\n")
def main():
    data = [int(e) for e in data]

    a = np.diff(data)
    print("Solution to first part: ", np.where(a > 0, 1, 0).sum())



    a = np.convolve(data, np.ones(3,dtype=int),'valid')
    a = np.diff(a)
    print("Solution to second part: ", np.where(a > 0, 1, 0).sum())


def streamlit_torch_1(day_input):
    """
    Day 1 solution for AoC using PyTorch
    """
    import torch
    import streamlit as st
    data = day_input.rstrip().split(" ")
    data = torch.tensor([int(e) for e in data])

    a = torch.diff(data)
    st.write("Solution to first part: ", torch.where(a > 0, 1, 0).sum())

    # Computing a moving window sum as a difference between two
    # cumulative sums starting at different places.
    # We also need to insert the first value of the cumulative sum
    # that's why you see the weird cat and view.
    a = torch.cumsum(data, axis=0)
    first_element = a[0]
    a = (a[3:] - a[:-3])
    a = torch.cat((first_element.view(1), a))
    a = torch.diff(a)
    st.write("Solution to second part: ", torch.where(a > 0, 1, 0).sum())


def streamlit_1(day_input):
    """
    Day 1 solution for AoC
    """
    import numpy as np
    import streamlit as st
    data = day_input.rstrip().split(" ")
    data = [int(e) for e in data]

    a = np.diff(data)
    st.write("Solution to first part: ", np.where(a > 0, 1, 0).sum())



    a = np.convolve(data, np.ones(3,dtype=int),'valid')
    a = np.diff(a)
    st.write("Solution to second part: ", np.where(a > 0, 1, 0).sum())


if __name__ == "__main__":
    main()