-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
121 lines (87 loc) · 3.68 KB
/
app.py
File metadata and controls
121 lines (87 loc) · 3.68 KB
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
113
114
115
116
117
118
119
120
121
import streamlit as st
import requests
from datetime import datetime
from importlib import import_module
from params import years, days, headers
from random import randint
st.title("AOC Interface")
#########################
## Obtaining user info ##
#########################
today = datetime.now().day
col1, col2 = st.columns(2)
with col1:
year = st.selectbox("Choose year:", reversed(years.keys()), index=0)
with col2:
if int(year) == datetime.now().year:
this_day = min(25, datetime.now().day)
options = list(days.keys())[this_day-1::-1]
day = st.selectbox("Choose day:", options, index=0)
st.columns(1)
session_cookie = st.text_input(label="[Optional] Your session cookie (for automatic retrieval and answering):").strip()
url = f"https://adventofcode.com/{year}/day/{day}/input"
st.write(f"Go get your puzzle input here, and copy the input: {url}")
if session_cookie:
cookies = {'session': session_cookie}
result = requests.get(url,
cookies=cookies,
headers=headers)
lines = result.text.strip('\n')
else:
lines = st.text_area("Copy the contents of your input file in here and press Ctrl+Enter:").strip('\n')
#########################
## Solve the puzzle ##
#########################
module_name = f'aoc{year[-2:]}.day{day}.solve'
try:
solver = import_module(module_name)
except:
st.error(f"Solution solver not found. Ask Jules ;-).\n{module_name}")
if len(lines) > 1:
one, two = solver.solve(lines)
st.subheader("Solutions to the puzzles")
st.markdown(f"The result for part one is *{one}*.")
st.markdown(f"The result for part two is *{two}*.")
st.markdown("\n")
#########################
## Submit to AOC site ##
#########################
if session_cookie:
url_answer = f"https://adventofcode.com/{year}/day/{day}/answer"
parts = [
{
'level': '1',
'answer': str(one),
'ord': 'first'
},
{
'level': '2',
'answer': str(two),
'ord': 'second'
},
]
for part in parts:
if st.button(f"Click to submit part {part['level']}"):
if part['level'] == '2':
if 'one_time' not in st.session_state:
st.warning("It would make sense to first submit the first part, no?")
else:
delta = datetime.now() - st.session_state['one_time']
if delta.seconds < randint(5*60, 10*60):
st.warning("That's a bit quick, don't you think?\nTry again in a few minutes.")
else:
st.session_state['one_time'] = datetime.now()
st.subheader(f"Answers from the AOC website for the {part['ord']} part")
data = {'level': part['level'], 'answer': part['answer']}
part['result'] = requests.post(url=url_answer,
data=data,
cookies=cookies,
headers=headers)
if "That's the" in part['result'].text:
st.write(f"Congratulations. You solved the {part['ord']} puzzle...")
if "That's not the right answer." in part['result'].text:
st.write("Oops. Seems to be the wrong answer.")
if "Did you already complete it?" in part['result'].text:
st.write("Did you already complete this puzzle?")
with st.expander("Open to see full response."):
st.code(part['result'].text)