File size: 4,049 Bytes
0f06ae9
 
 
 
 
 
 
1cde1c2
 
 
 
 
0f06ae9
 
 
 
 
 
1cde1c2
0f06ae9
 
 
 
 
1cde1c2
 
 
 
 
 
0f06ae9
1cde1c2
0f06ae9
1cde1c2
 
 
 
 
 
0f06ae9
1cde1c2
 
0f06ae9
1cde1c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f06ae9
1cde1c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import streamlit as st
import os
import importlib
import inspect
import numpy as np
import torch
from PIL import Image
# from aoc.year_2021.code.day_1 import streamlit_1, streamlit_torch_1
# from aoc.year_2021.code.day_2 import streamlit_2
# from aoc.year_2021.code.day_3 import streamlit_torch_3
# from aoc.year_2021.code.day_4 import streamlit_4
# from aoc.year_2021.code.day_5 import streamlit_5


st.sidebar.markdown("**AoC 2021 app** by Yassine Alouini")
logo = Image.open('logo.png')
st.sidebar.image(logo, width=64)

day = st.sidebar.selectbox("Select the day: ", [5])
day_input = st.sidebar.text_area("Paste your input here: ", "")
show_code = st.sidebar.radio("Show code? ", [True, False])
show_torch_code = st.sidebar.radio("Show PyTorch code? ", [True, False])


def streamlit_5(data_input):
    """ Day 5 solution (mainly using numpy)
    """
    import numpy as np
    import re
    import streamlit as st

    tmp = data_input.rstrip().split("\n")

    # Using complex numbers as (x, y) representation
    starts, ends = [], []
    for row in tmp:
        x1, y1, x2, y2 = re.findall(r'\d+', row)
        starts.append(int(x1) + 1j * int(y1))
        ends.append(int(x2) + 1j * int(y2))

    # Dimension of the grid
    d = max(map(abs, starts)) + 1

    def solve(part_2=False):
        a = np.zeros((int(d), int(d)))
        for start_point, end_point in zip(starts, ends):
            start_x = min(int(start_point.real), int(end_point.real))
            end_x = max(int(start_point.real), int(end_point.real))
            start_y = min(int(start_point.imag), int(end_point.imag))
            end_y = max(int(start_point.imag), int(end_point.imag))
            # Compute cosine and sine to find if diagonal or anti-diagonal
            diff = start_point - end_point
            c = (diff.real) / abs(diff)
            s = (diff.imag) / abs(diff)
            sliced_a = a[start_x: end_x + 1, start_y: end_y + 1]
            criterion = round(s * c, 1)
            if part_2:
                if criterion == 0.5:
                    np.fill_diagonal(sliced_a, sliced_a.diagonal() + 1)
                elif criterion == -0.5:
                    # Need to flip the sliced matrix to get the correct diagonal
                    np.fill_diagonal(np.fliplr(sliced_a), 
                                     np.fliplr(sliced_a).diagonal() + 1)
            # Either horizontal or vertical
            if criterion == 0:
                sliced_a += 1
        return a
    
    st.write("Solution to part I: ", (solve()>= 2).sum())
    st.write("Solution to part II: ", (solve(part_2=True)>= 2).sum())



# if day == 1 and (day_input is not None and day_input != ""):
#     if show_code:
#         st.code(inspect.getsource(streamlit_1))
#         streamlit_1(day_input)
#     if show_torch_code:
#         st.code(inspect.getsource(streamlit_torch_1))
#         streamlit_torch_1(day_input)

# if day == 2 and (day_input is not None and day_input != ""):
#     if show_code:
#         st.code(inspect.getsource(streamlit_2))
#         streamlit_2(day_input)
#     # if show_torch_code:
#     #     st.code(inspect.getsource(day_1_torch))
#     #     day_1_torch(day_input)

# if day == 3 and (day_input is not None and day_input != ""):
#     # if show_code:
#     #     st.code(inspect.getsource(streamlit_3))
#     #     streamlit_2(day_input)
#     if show_torch_code:
#         st.code(inspect.getsource(streamlit_torch_3))
#         streamlit_torch_3(day_input)

# if day == 4 and (day_input is not None and day_input != ""):
#     if show_code:
#         st.code(inspect.getsource(streamlit_4))
#         streamlit_4(day_input)
#     # if show_torch_code:
#     #     st.code(inspect.getsource(streamlit_torch_3))
#     #     streamlit_torch_3(day_input)

if day == 5 and (day_input is not None and day_input != ""):
    if show_code:
        st.code(inspect.getsource(streamlit_5))
        streamlit_5(day_input)
    # if show_torch_code:
    #     st.code(inspect.getsource(streamlit_torch_3))
    #     streamlit_torch_3(day_input)