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()