aoc-2020-zig

git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

commit 7a0da710b68026ad2096fc9b693c7690849960ff
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 30 Nov 2020 19:13:12 +0100

added prepare scripts

Diffstat:
A.gitignore | 1+
AREADME | 4++++
Aaoc | 36++++++++++++++++++++++++++++++++++++
Ascripts/prepare | 40++++++++++++++++++++++++++++++++++++++++
Ascripts/scrape | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/submit | 22++++++++++++++++++++++
6 files changed, 181 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +data/cookie diff --git a/README b/README @@ -0,0 +1,4 @@ +Advent Of Code 2020 +=================== + + diff --git a/aoc b/aoc @@ -0,0 +1,36 @@ +#!/bin/bash + +function rmprefix() { + echo "${1:$(echo -n "$2" | wc -c)}" +} + +SCRIPTPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +REPOROOT=$(git -C "$SCRIPTPATH" rev-parse --show-toplevel) + +if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then + if [ -z "$PS1_PREFIX" ]; then + echo "Enabling AoC env.." + + export PS1_PREFIX="[🎄]:" + export PS1="$PS1_PREFIX$PS1" + + export PATH_PREFIX="$REPOROOT:" + export PATH="$PATH_PREFIX$PATH" + + export REPOROOT="$REPOROOT" + else + echo "Disabling AoC env.." + + export PS1=$(rmprefix "$PS1" "$PS1_PREFIX") + export PS1_PREFIX= + + export PATH=$(rmprefix "$PATH" "$PATH_PREFIX") + export PATH_PREFIX= + + export REPOROOT= + fi +else + [ $# -lt 1 ] && exit 0 + + "$REPOROOT/scripts/$1" ${@:2} +fi diff --git a/scripts/prepare b/scripts/prepare @@ -0,0 +1,40 @@ +#!/bin/bash + +if [ $# -ne 1 ]; then + echo "Usage: prepare <daynum>" + exit 1 +fi + +function load() { + echo -n "$1.." + $REPOROOT/scripts/scrape ${@:2} + rv=$? + [ $rv -eq 0 ] && echo "done" || echo "fail" + return $rv +} + +set -e + +if [[ "$1" =~ "[0-9]+" ]]; then + echo "Not a number" + exit 1 +fi + +daynum="$1" +folder="$REPOROOT/day$daynum" +if [ -e "$folder" ]; then + echo "Directory for problem already exists" + exit 1 +fi + +echo "Initializing folder" + +mkdir $folder +pushd $reporoot$folder &>/dev/null + +load "Loading problem input" $daynum input +load "Loading first problem text" $daynum text 1 +load "Loading second problem text" $daynum text 1 + +popd &>/dev/null + diff --git a/scripts/scrape b/scripts/scrape @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 + +import requests, sys, os, json +from termcolor import colored +from bs4 import BeautifulSoup, element + +if len(sys.argv) < 3: + print("Usage: scrape <day> <cmd> [<args..>]") + sys.exit(1) + +day = int(sys.argv[1]) +cmd = sys.argv[2] +cmdargs = None if len(sys.argv) == 3 else sys.argv[3:] + +year = "2020" +colortext = True +reporoot = os.getenv("REPOROOT") +headers = json.loads(open(f"{reporoot}/data/headers").read()) +baseurl = f"https://adventofcode.com/{year}/day/{day}" + +def scrape_input(day): + path = f"{reporoot}/day{day}/input" + if os.path.exists(path): + sys.exit(1) + + r = requests.get(f"{baseurl}/input", headers = headers) + + if r.status_code != 200: + sys.exit(1) + + with open(path, "w+") as f: + f.write(r.text) + +def extract_hltext(elem): + text = "" + for ce in elem: + if ce.name == "em": + if not ce.get("class"): + text += colored(ce.text, "white", attrs=["bold"]) + elif "star" in ce.get("class"): + text += colored(ce.text, "yellow", attrs=["bold"]) + else: + text += colored(ce.text, "magenta", attrs=["bold"]) + elif type(ce) == element.NavigableString: + text += str(ce) + elif ce.name == "li": + text += " - " + extract_hltext(ce) + elif ce.name[0] == "h": + text += ce.text + "\n\n" + elif ce.name == "p" or ce.name == "ul": + text += extract_hltext(ce) + "\n" + else: + text += extract_hltext(ce) + return text + +def scrape_text(day, part): + path = f"{reporoot}/day{day}/part{part}" + if os.path.exists(path): + sys.exit(1) + + r = requests.get(baseurl, headers = headers) + soup = BeautifulSoup(r.text, "html.parser") + + textobj = soup.select("article.day-desc")[part - 1] + + with open(path, "w+") as of: + of.write("{}\n".format(extract_hltext(textobj))) + +def main(): + if cmd == "input": + scrape_input(day) + elif cmd == "text": + part = int(cmdargs[0]) if cmdargs else 1 + scrape_text(day, part) + else: + sys.exit(1) + +main() diff --git a/scripts/submit b/scripts/submit @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import requests, sys, os, json +from bs4 import BeautifulSoup + +if len(sys.argv) != 4: + print("Usage: submit <day> <part> <answer>") + sys.exit(1) + +daynum = sys.argv[1] +lvlnum = sys.argv[2] +answer = sys.argv[3] + +year = "2020" +url = f"https://adventofcode.com/{year}/day/{daynum}/answer" +reporoot = os.getenv("REPOROOT") +headers = json.loads(open(f"{reporoot}/data/headers").read()) +data = { 'level': lvlnum, 'answer': answer } + +r = requests.post(url, headers=headers, data=data) +soup = BeautifulSoup(r.text, "html.parser") +print(soup.select_one("main").text)