YassineAlouini
commited on
Commit
·
1cde1c2
1
Parent(s):
0f06ae9
Day 5 in the app file.
Browse files
app.py
CHANGED
@@ -5,58 +5,103 @@ import inspect
|
|
5 |
import numpy as np
|
6 |
import torch
|
7 |
from PIL import Image
|
8 |
-
from aoc.year_2021.code.day_1 import streamlit_1, streamlit_torch_1
|
9 |
-
from aoc.year_2021.code.day_2 import streamlit_2
|
10 |
-
from aoc.year_2021.code.day_3 import streamlit_torch_3
|
11 |
-
from aoc.year_2021.code.day_4 import streamlit_4
|
12 |
-
from aoc.year_2021.code.day_5 import streamlit_5
|
13 |
|
14 |
|
15 |
st.sidebar.markdown("**AoC 2021 app** by Yassine Alouini")
|
16 |
logo = Image.open('logo.png')
|
17 |
st.sidebar.image(logo, width=64)
|
18 |
|
19 |
-
day = st.sidebar.selectbox("Select the day: ", [
|
20 |
day_input = st.sidebar.text_area("Paste your input here: ", "")
|
21 |
show_code = st.sidebar.radio("Show code? ", [True, False])
|
22 |
show_torch_code = st.sidebar.radio("Show PyTorch code? ", [True, False])
|
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 |
if day == 5 and (day_input is not None and day_input != ""):
|
62 |
if show_code:
|
|
|
5 |
import numpy as np
|
6 |
import torch
|
7 |
from PIL import Image
|
8 |
+
# from aoc.year_2021.code.day_1 import streamlit_1, streamlit_torch_1
|
9 |
+
# from aoc.year_2021.code.day_2 import streamlit_2
|
10 |
+
# from aoc.year_2021.code.day_3 import streamlit_torch_3
|
11 |
+
# from aoc.year_2021.code.day_4 import streamlit_4
|
12 |
+
# from aoc.year_2021.code.day_5 import streamlit_5
|
13 |
|
14 |
|
15 |
st.sidebar.markdown("**AoC 2021 app** by Yassine Alouini")
|
16 |
logo = Image.open('logo.png')
|
17 |
st.sidebar.image(logo, width=64)
|
18 |
|
19 |
+
day = st.sidebar.selectbox("Select the day: ", [5])
|
20 |
day_input = st.sidebar.text_area("Paste your input here: ", "")
|
21 |
show_code = st.sidebar.radio("Show code? ", [True, False])
|
22 |
show_torch_code = st.sidebar.radio("Show PyTorch code? ", [True, False])
|
23 |
|
24 |
|
25 |
+
def streamlit_5(data_input):
|
26 |
+
""" Day 5 solution (mainly using numpy)
|
27 |
+
"""
|
28 |
+
import numpy as np
|
29 |
+
import re
|
30 |
+
import streamlit as st
|
31 |
|
32 |
+
tmp = data_input.rstrip().split("\n")
|
33 |
|
34 |
+
# Using complex numbers as (x, y) representation
|
35 |
+
starts, ends = [], []
|
36 |
+
for row in tmp:
|
37 |
+
x1, y1, x2, y2 = re.findall(r'\d+', row)
|
38 |
+
starts.append(int(x1) + 1j * int(y1))
|
39 |
+
ends.append(int(x2) + 1j * int(y2))
|
40 |
|
41 |
+
# Dimension of the grid
|
42 |
+
d = max(map(abs, starts)) + 1
|
43 |
|
44 |
+
def solve(part_2=False):
|
45 |
+
a = np.zeros((int(d), int(d)))
|
46 |
+
for start_point, end_point in zip(starts, ends):
|
47 |
+
start_x = min(int(start_point.real), int(end_point.real))
|
48 |
+
end_x = max(int(start_point.real), int(end_point.real))
|
49 |
+
start_y = min(int(start_point.imag), int(end_point.imag))
|
50 |
+
end_y = max(int(start_point.imag), int(end_point.imag))
|
51 |
+
# Compute cosine and sine to find if diagonal or anti-diagonal
|
52 |
+
diff = start_point - end_point
|
53 |
+
c = (diff.real) / abs(diff)
|
54 |
+
s = (diff.imag) / abs(diff)
|
55 |
+
sliced_a = a[start_x: end_x + 1, start_y: end_y + 1]
|
56 |
+
criterion = round(s * c, 1)
|
57 |
+
if part_2:
|
58 |
+
if criterion == 0.5:
|
59 |
+
np.fill_diagonal(sliced_a, sliced_a.diagonal() + 1)
|
60 |
+
elif criterion == -0.5:
|
61 |
+
# Need to flip the sliced matrix to get the correct diagonal
|
62 |
+
np.fill_diagonal(np.fliplr(sliced_a),
|
63 |
+
np.fliplr(sliced_a).diagonal() + 1)
|
64 |
+
# Either horizontal or vertical
|
65 |
+
if criterion == 0:
|
66 |
+
sliced_a += 1
|
67 |
+
return a
|
68 |
+
|
69 |
+
st.write("Solution to part I: ", (solve()>= 2).sum())
|
70 |
+
st.write("Solution to part II: ", (solve(part_2=True)>= 2).sum())
|
71 |
|
72 |
+
|
73 |
+
|
74 |
+
# if day == 1 and (day_input is not None and day_input != ""):
|
75 |
+
# if show_code:
|
76 |
+
# st.code(inspect.getsource(streamlit_1))
|
77 |
+
# streamlit_1(day_input)
|
78 |
+
# if show_torch_code:
|
79 |
+
# st.code(inspect.getsource(streamlit_torch_1))
|
80 |
+
# streamlit_torch_1(day_input)
|
81 |
+
|
82 |
+
# if day == 2 and (day_input is not None and day_input != ""):
|
83 |
+
# if show_code:
|
84 |
+
# st.code(inspect.getsource(streamlit_2))
|
85 |
+
# streamlit_2(day_input)
|
86 |
+
# # if show_torch_code:
|
87 |
+
# # st.code(inspect.getsource(day_1_torch))
|
88 |
+
# # day_1_torch(day_input)
|
89 |
+
|
90 |
+
# if day == 3 and (day_input is not None and day_input != ""):
|
91 |
+
# # if show_code:
|
92 |
+
# # st.code(inspect.getsource(streamlit_3))
|
93 |
+
# # streamlit_2(day_input)
|
94 |
+
# if show_torch_code:
|
95 |
+
# st.code(inspect.getsource(streamlit_torch_3))
|
96 |
+
# streamlit_torch_3(day_input)
|
97 |
+
|
98 |
+
# if day == 4 and (day_input is not None and day_input != ""):
|
99 |
+
# if show_code:
|
100 |
+
# st.code(inspect.getsource(streamlit_4))
|
101 |
+
# streamlit_4(day_input)
|
102 |
+
# # if show_torch_code:
|
103 |
+
# # st.code(inspect.getsource(streamlit_torch_3))
|
104 |
+
# # streamlit_torch_3(day_input)
|
105 |
|
106 |
if day == 5 and (day_input is not None and day_input != ""):
|
107 |
if show_code:
|