|
import streamlit as st |
|
import os |
|
import importlib |
|
import inspect |
|
import numpy as np |
|
import torch |
|
from PIL import Image |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
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)) |
|
|
|
|
|
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)) |
|
|
|
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: |
|
|
|
np.fill_diagonal(np.fliplr(sliced_a), |
|
np.fliplr(sliced_a).diagonal() + 1) |
|
|
|
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 == 5 and (day_input is not None and day_input != ""): |
|
if show_code: |
|
st.code(inspect.getsource(streamlit_5)) |
|
streamlit_5(day_input) |
|
|
|
|
|
|