-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathadvent.py
164 lines (127 loc) · 3.99 KB
/
advent.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import sys
import re
from importlib import find_loader
from datetime import datetime, timedelta
from time import sleep
def log(s, *a):
sys.stderr.write('[advent] ' + s.format(*a))
sys.stderr.flush()
def logcont(s, *a):
sys.stderr.write(s.format(*a))
sys.stderr.flush()
def check_or_die(resp):
if resp.status_code != 200:
logcont('\n')
log('ERROR: response {}, url: {}\n', resp.status_code, resp.url)
log('Did you log in and update your session cookie?\n')
sys.exit(1)
if 'please identify yourself' in resp.text.lower():
logcont('\n')
log('ERROR: Server returned 200, but is asking for identification.\n')
log('Did you log in and update your session cookie?\n')
sys.exit(1)
def check_setup_once():
if YEAR == -1 and DAY == -1:
now = datetime.utcnow() + timedelta(hours=-5)
y, m, d = now.year, now.month, now.day
if m != 12 or (m == 12 and d > 25):
log('ERROR: year and day not set, and no event currently running!\n')
sys.exit(1)
log('Year and day not set, assuming today: Dec {}, {}.\n', d, y)
setup(y, d)
def setup(year, day):
global YEAR
global DAY
global SESSION
if not (year >= 2015 and 1 <= day <= 25):
log('ERROR: invalid year and/or day set!\n')
sys.exit(1)
YEAR = year
DAY = day
if REQUESTS and os.path.isfile('secret_session_cookie'):
with open('secret_session_cookie') as f:
SESSION = f.read().rstrip()
S.cookies.set('session', SESSION)
def get_input(fname=None, mode='r'):
check_setup_once()
if fname is not None:
return open(fname, mode)
if not os.path.isdir(CACHE_DIR):
try:
os.mkdir(CACHE_DIR)
log("Created cache directory '{}' since it did not exist.\n", CACHE_DIR)
except Exception as e:
log("ERROR: could not create cache directory '{}'.\n", CACHE_DIR)
log('{}\n', str(e))
sys.exit(1)
log('Getting input for year {} day {}... ', YEAR, DAY)
fname = os.path.join(CACHE_DIR, '{}_{:02d}.txt'.format(YEAR, DAY))
try:
file = open(fname, mode)
logcont('done (from disk).\n')
return file
except FileNotFoundError:
pass
if not REQUESTS:
logcont('err!\n')
log('ERROR: cannot download input, no requests module installed!\n')
sys.exit(1)
elif not SESSION:
logcont('err!\n')
log('ERROR: cannot download input file without session cookie!\n')
sys.exit(1)
logcont('downloading... ')
r = S.get(URL.format(YEAR, DAY, 'input'))
check_or_die(r)
with open(fname, 'wb') as f:
f.write(r.content)
file = open(fname, mode)
logcont('done.\n')
return file
def print_answer(part, answer):
print('Part {}:'.format(part), answer)
def submit_answer(part, answer):
check_setup_once()
if not REQUESTS:
log('Cannot upload answer, no requests module installed!\n')
print_answer(part, answer)
return False
log('Submitting day {} part {} answer: {}\n', DAY, part, answer)
r = S.post(URL.format(YEAR, DAY, 'answer'), data={'level': part, 'answer': answer})
check_or_die(r)
t = r.text.lower()
if 'did you already complete it' in t:
log('Already completed or wrong day/part.\n')
return False
if "that's the right answer" in t:
matches = re.findall(r'rank\s+(\d+)', t)
if matches:
logcont('Right answer! Rank {}.\n', matches[0])
else:
log('Right answer!\n')
if DAY == 25 and part == 1:
log("It's Christmas! Automatically submitting second part...\n")
S.post(URL.format(YEAR, 25, 'answer'), data={'level': 2, 'answer': 0})
logcont('done!\n')
log('Go check it out: https://adventofcode.com/{}/day/25#part2\n', YEAR)
return True
if 'you have to wait' in t:
matches = re.compile(r'you have ([\w ]+) left to wait').findall(t)
if matches:
log('Submitting too fast, {} left to wait.\n', matches[0])
else:
log('Submitting too fast, slow down!\n')
return False
log('Wrong answer :(\n')
return False
URL = 'https://adventofcode.com/{:d}/day/{:d}/{:s}'
SESSION = ''
CACHE_DIR = '../inputs'
YEAR = -1
DAY = -1
REQUESTS = find_loader('requests')
if REQUESTS:
import requests
S = requests.Session()
S.headers['User-Agent'] = 'github.com/mebeim/aoc by marco AT mebeim.net'