aoc / app.py
YassineAlouini
Day 5 in the app file.
1cde1c2
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)