-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
name: Get inputs | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
year: | ||
required: false | ||
type: number | ||
secrets: | ||
SESSION: | ||
required: true | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Get Day | ||
id: get-day | ||
run: | | ||
from datetime import datetime, timedelta, tzinfo | ||
class TZ(tzinfo): | ||
def utcoffset(self, dt): | ||
return timedelta(hours=-5) + self.dst(dt) | ||
def dst(self, dt): | ||
return timedelta(0) | ||
year = ${{ inputs.year }} or 2022 | ||
day = max(0, min(25, (datetime.now(TZ()) - datetime(year, 12, 1, 0, 0, 0, 0, TZ())).days + 1)) | ||
print("::set-output name=year::" + str(year)) | ||
print("::set-output name=day::" + str(min(25, max(0, day)))) | ||
print("::set-output name=days::" + ' '.join(map(str, range(1, day + 1)))) | ||
shell: python | ||
- id: cache | ||
uses: actions/cache@v2 | ||
with: | ||
key: inputs-${{ steps.get-day.outputs.day }} | ||
restore-keys: inputs- | ||
path: day*.txt | ||
- name: Download inputs | ||
if: steps.cache.outputs.cache-hit != 'true' | ||
run: | | ||
for day in ${{ steps.get-day.outputs.days }}; do | ||
[[ -e day$day.txt ]] || curl -b session=$SESSION -o day$day.txt -f https://adventofcode.com/${{ steps.get-day.outputs.year }}/day/$day/input | ||
done | ||
shell: bash --noprofile --norc -euxo pipefail {0} | ||
env: | ||
SESSION: ${{ secrets.SESSION }} | ||
- uses: actions/upload-artifact@v2 | ||
with: | ||
name: inputs | ||
path: day*.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/day*.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# [Advent of Code 2022](https://adventofcode.com/2022) | ||
### my answers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/usr/bin/env python3 | ||
import argparse | ||
from datetime import datetime, timedelta, tzinfo | ||
from pathlib import Path | ||
import shutil | ||
import sys | ||
import time | ||
from urllib.error import HTTPError | ||
from urllib.request import urlopen, Request | ||
|
||
|
||
class TZ(tzinfo): | ||
|
||
def utcoffset(self, dt): | ||
return timedelta(hours=-5) + self.dst(dt) | ||
|
||
def dst(self, dt): | ||
return timedelta(0) | ||
|
||
|
||
def main(): | ||
terminal_output = sys.stdout if sys.stdout.isatty( | ||
) else sys.stderr if sys.stderr.isatty() else None | ||
|
||
parser = argparse.ArgumentParser() | ||
group = parser.add_mutually_exclusive_group() | ||
group.add_argument("-n", "--dry-run", action='store_true') | ||
group.add_argument("-o", "--overwrite", action='store_true', dest='force') | ||
group = parser.add_mutually_exclusive_group() | ||
group.add_argument("-s", "--session") | ||
group.add_argument("-S", | ||
"--session-file", | ||
metavar='FILE', | ||
type=argparse.FileType()) | ||
parser.add_argument("-y", "--year", type=int, default=2022) | ||
parser.add_argument("days", metavar='DAY', type=int, nargs='*') | ||
args = parser.parse_args() | ||
|
||
dry_run, force, session = args.dry_run, args.force, args.session | ||
if not session: | ||
session_file = args.session_file | ||
if not session_file: | ||
session_file = open(Path.home() / '.aocrc') | ||
with session_file: | ||
session = session_file.read().strip() | ||
|
||
year, days = args.year, args.days | ||
base = datetime(year, 12, 1, 0, 0, 0, 0, TZ()) | ||
if days: | ||
days = sorted(set(days)) | ||
else: | ||
days = (datetime.now(TZ()) - base).days + 1 | ||
days = range(1, max(0, min(25, days))) | ||
|
||
for day in days: | ||
file = f"day{day}.txt" | ||
if not force and Path(file).exists(): | ||
print(f"{file} already exists") | ||
continue | ||
if not dry_run: | ||
target = base + timedelta(days=day - 1) | ||
while True: | ||
now = datetime.now(TZ()) | ||
if now >= target: | ||
break | ||
delta = target - now | ||
message = f"{file} available in {delta}" | ||
if terminal_output: | ||
print(message, end='', file=terminal_output, flush=True) | ||
if delta > timedelta(hours=2): | ||
delta = delta % timedelta(hours=1) | ||
elif delta > timedelta(minutes=2): | ||
delta = delta % timedelta(minutes=1) | ||
elif delta > timedelta(seconds=2): | ||
delta = delta % timedelta(seconds=1) | ||
elif delta > timedelta(milliseconds=20): | ||
delta = delta % timedelta(milliseconds=10) | ||
time.sleep(delta.total_seconds()) | ||
print('\033[2K\r', | ||
end='', | ||
file=terminal_output, | ||
flush=True) | ||
else: | ||
print(message) | ||
time.sleep(delta.total_seconds()) | ||
url = f"https://www.adventofcode.com/{year}/day/{day}/input" | ||
print(f"{file} = {url}") | ||
if dry_run: | ||
continue | ||
request = Request(url, headers={"Cookie": f"session={session}"}) | ||
response = urlopen(request) | ||
if response.status != 200: | ||
raise HTTPError(response.url, response.code, response.msg, | ||
response.headers, None) | ||
with response: | ||
with open(file, 'wb') as f: | ||
shutil.copyfileobj(response, f) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |