initial commit

This commit is contained in:
Barrett Ruth 2025-09-11 23:52:32 -05:00
commit dcb7debff6
29 changed files with 1276 additions and 0 deletions

9
templates/.clang-format Normal file
View file

@ -0,0 +1,9 @@
BasedOnStyle: Google
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortCompoundRequirementOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: false
AllowShortLoopsOnASingleLine: false

33
templates/.clangd Normal file
View file

@ -0,0 +1,33 @@
CompileFlags:
Add:
-O2
-Wall
-Wextra
-Wpedantic
-Wshadow
-Wformat=2
-Wfloat-equal
-Wlogical-op
-Wshift-overflow=2
-Wnon-virtual-dtor
-Wold-style-cast
-Wcast-qual
-Wuseless-cast
-Wno-sign-promotion
-Wcast-align
-Wunused
-Woverloaded-virtual
-Wconversion
-Wsign-conversion
-Wmisleading-indentation
-Wduplicated-cond
-Wduplicated-branches
-Wlogical-op
-Wnull-dereference
-Wformat=2
-Wformat-overflow
-Wformat-truncation
-Wdouble-promotion
-Wundef
-DLOCAL
-Wno-unknown-pragmas

View file

@ -0,0 +1,2 @@
-O2
-DLOCAL

View file

@ -0,0 +1,3 @@
-g3
-fsanitize=address,undefined
-DLOCAL

31
templates/makefile Normal file
View file

@ -0,0 +1,31 @@
.PHONY: run debug clean setup init scrape
VERSION ?= 20
SRC = $(word 2,$(MAKECMDGOALS))
.SILENT:
run:
sh scripts/run.sh $(SRC)
debug:
sh scripts/debug.sh $(SRC)
clean:
rm -rf build/*
setup:
test -d build || mkdir -p build
test -d io || mkdir -p io
test -f compile_flags.txt && echo -std=c++$(VERSION) >>compile_flags.txt
test -f .clangd && echo -e "\t\t-std=c++$(VERSION)" >>.clangd
init:
make setup
scrape:
sh scripts/scrape.sh $(word 2,$(MAKECMDGOALS)) $(word 3,$(MAKECMDGOALS)) $(word 4,$(MAKECMDGOALS))
%:
@:

View file

@ -0,0 +1,87 @@
#!/usr/bin/env python3
import sys
import requests
from bs4 import BeautifulSoup
def parse_problem_url(contest_id: str, problem_letter: str) -> str:
task_id = f"{contest_id}_{problem_letter}"
return f"https://atcoder.jp/contests/{contest_id}/tasks/{task_id}"
def scrape(url: str) -> list[tuple[str, str]]:
try:
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
tests = []
sample_headers = soup.find_all(
"h3", string=lambda x: x and "sample" in x.lower() if x else False
)
i = 0
while i < len(sample_headers):
header = sample_headers[i]
if "input" in header.get_text().lower():
input_pre = header.find_next("pre")
if input_pre and i + 1 < len(sample_headers):
next_header = sample_headers[i + 1]
if "output" in next_header.get_text().lower():
output_pre = next_header.find_next("pre")
if output_pre:
input_text = input_pre.get_text().strip().replace("\r", "")
output_text = (
output_pre.get_text().strip().replace("\r", "")
)
if input_text and output_text:
tests.append((input_text, output_text))
i += 2
continue
i += 1
return tests
except Exception as e:
print(f"Error scraping AtCoder: {e}", file=sys.stderr)
return []
def main():
if len(sys.argv) != 3:
print("Usage: atcoder.py <contest_id> <problem_letter>", file=sys.stderr)
print("Example: atcoder.py abc042 a", file=sys.stderr)
sys.exit(1)
contest_id = sys.argv[1]
problem_letter = sys.argv[2]
url = parse_problem_url(contest_id, problem_letter)
print(f"Scraping: {url}", file=sys.stderr)
tests = scrape(url)
if not tests:
print(f"No tests found for {contest_id} {problem_letter}", file=sys.stderr)
sys.exit(1)
print("---INPUT---")
print(len(tests))
for input_data, output_data in tests:
print(input_data)
print("---OUTPUT---")
for input_data, output_data in tests:
print(output_data)
print("---END---")
if __name__ == "__main__":
main()

View file

@ -0,0 +1,104 @@
#!/usr/bin/env python3
import sys
import cloudscraper
from bs4 import BeautifulSoup
def scrape(url: str):
try:
scraper = cloudscraper.create_scraper()
response = scraper.get(url, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
tests = []
input_sections = soup.find_all("div", class_="input")
output_sections = soup.find_all("div", class_="output")
for inp_section, out_section in zip(input_sections, output_sections):
inp_pre = inp_section.find("pre")
out_pre = out_section.find("pre")
if inp_pre and out_pre:
input_lines = []
output_lines = []
for line_div in inp_pre.find_all("div", class_="test-example-line"):
input_lines.append(line_div.get_text().strip())
output_divs = out_pre.find_all("div", class_="test-example-line")
if not output_divs:
output_text_raw = out_pre.get_text().strip().replace("\r", "")
output_lines = [
line.strip()
for line in output_text_raw.split("\n")
if line.strip()
]
else:
for line_div in output_divs:
output_lines.append(line_div.get_text().strip())
if input_lines and output_lines:
if len(input_lines) > 1 and input_lines[0].isdigit():
test_count = int(input_lines[0])
remaining_input = input_lines[1:]
for i in range(min(test_count, len(output_lines))):
if i < len(remaining_input):
tests.append((remaining_input[i], output_lines[i]))
else:
input_text = "\n".join(input_lines)
output_text = "\n".join(output_lines)
tests.append((input_text, output_text))
return tests
except Exception as e:
print(f"CloudScraper failed: {e}", file=sys.stderr)
return []
def parse_problem_url(contest_id: str, problem_letter: str) -> str:
return (
f"https://codeforces.com/contest/{contest_id}/problem/{problem_letter.upper()}"
)
def scrape_sample_tests(url: str):
print(f"Scraping: {url}", file=sys.stderr)
return scrape(url)
def main():
if len(sys.argv) != 3:
print("Usage: codeforces.py <contest_id> <problem_letter>", file=sys.stderr)
print("Example: codeforces.py 1234 A", file=sys.stderr)
sys.exit(1)
contest_id = sys.argv[1]
problem_letter = sys.argv[2]
url = parse_problem_url(contest_id, problem_letter)
tests = scrape_sample_tests(url)
if not tests:
print(f"No tests found for {contest_id} {problem_letter}", file=sys.stderr)
print(
"Consider adding test cases manually to the io/ directory", file=sys.stderr
)
sys.exit(1)
print("---INPUT---")
print(len(tests))
for input_data, output_data in tests:
print(input_data)
print("---OUTPUT---")
for input_data, output_data in tests:
print(output_data)
print("---END---")
if __name__ == "__main__":
main()

88
templates/scrapers/cses.py Executable file
View file

@ -0,0 +1,88 @@
#!/usr/bin/env python3
import sys
import requests
from bs4 import BeautifulSoup
def parse_problem_url(problem_input: str) -> str | None:
if problem_input.startswith("https://cses.fi/problemset/task/"):
return problem_input
elif problem_input.isdigit():
return f"https://cses.fi/problemset/task/{problem_input}"
return None
def scrape(url: str) -> list[tuple[str, str]]:
try:
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
tests = []
example_header = soup.find("h1", string="Example")
if example_header:
current = example_header.find_next_sibling()
input_text = None
output_text = None
while current:
if current.name == "p" and "Input:" in current.get_text():
input_pre = current.find_next_sibling("pre")
if input_pre:
input_text = input_pre.get_text().strip()
elif current.name == "p" and "Output:" in current.get_text():
output_pre = current.find_next_sibling("pre")
if output_pre:
output_text = output_pre.get_text().strip()
break
current = current.find_next_sibling()
if input_text and output_text:
tests.append((input_text, output_text))
return tests
except Exception as e:
print(f"Error scraping CSES: {e}", file=sys.stderr)
return []
def main():
if len(sys.argv) != 2:
print("Usage: cses.py <problem_id_or_url>", file=sys.stderr)
sys.exit(1)
problem_input = sys.argv[1]
url = parse_problem_url(problem_input)
if not url:
print(f"Invalid problem input: {problem_input}", file=sys.stderr)
print("Use either problem ID (e.g., 1068) or full URL", file=sys.stderr)
sys.exit(1)
tests = scrape(url)
if not tests:
print(f"No tests found for {problem_input}", file=sys.stderr)
sys.exit(1)
print("---INPUT---")
print(len(tests))
for input_data, output_data in tests:
print(input_data)
print("---OUTPUT---")
for input_data, output_data in tests:
print(output_data)
print("---END---")
if __name__ == "__main__":
main()

View file

@ -0,0 +1,29 @@
#!/bin/sh
. ./scripts/utils.sh
SRC="$1"
BASE=$(basename "$SRC" .cc)
INPUT="${BASE}.in"
OUTPUT="${BASE}.out"
DBG_BIN="${BASE}.debug"
test -d build || mkdir -p build
test -d io || mkdir -p io
test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/"
test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/"
test -f "io/$INPUT" || touch "io/$INPUT"
test -f "io/$OUTPUT" || touch "io/$OUTPUT"
INPUT="io/$INPUT"
OUTPUT="io/$OUTPUT"
DBG_BIN="build/$DBG_BIN"
compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt
CODE=$?
test $CODE -gt 0 && exit $CODE
execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT" true
exit $?

29
templates/scripts/run.sh Normal file
View file

@ -0,0 +1,29 @@
#!/bin/sh
. ./scripts/utils.sh
SRC="$1"
BASE=$(basename "$SRC" .cc)
INPUT="${BASE}.in"
OUTPUT="${BASE}.out"
RUN_BIN="${BASE}.run"
test -d build || mkdir -p build
test -d io || mkdir -p io
test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/"
test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/"
test -f "io/$INPUT" || touch "io/$INPUT"
test -f "io/$OUTPUT" || touch "io/$OUTPUT"
INPUT="io/$INPUT"
OUTPUT="io/$OUTPUT"
RUN_BIN="build/$RUN_BIN"
compile_source "$SRC" "$RUN_BIN" "$OUTPUT" ""
CODE=$?
test $CODE -gt 0 && exit $CODE
execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT"
exit $?

85
templates/scripts/scrape.sh Executable file
View file

@ -0,0 +1,85 @@
#!/bin/sh
CONTEST="$1"
PROBLEM="$2"
PROBLEM_LETTER="$3"
if [ -z "$CONTEST" ] || [ -z "$PROBLEM" ]; then
echo "Usage: make scrape <contest> <problem_id> [problem_letter]"
echo "Available contests: cses, atcoder, codeforces"
echo "Examples:"
echo " make scrape cses 1068"
echo " make scrape atcoder abc042 a"
echo " make scrape codeforces 1234 A"
exit
fi
test -d io && true || mkdir -p io
TMPFILE=$(mktemp)
ORIGDIR=$(pwd)
case "$CONTEST" in
cses)
cd "$(dirname "$0")/../.." && uv run scrapers/cses.py "$PROBLEM" > "$TMPFILE"
if [ $? -eq 0 ]; then
cd "$ORIGDIR"
awk '/^---INPUT---$/ {getline; while ($0 != "---OUTPUT---") {print; getline}} END {}' "$TMPFILE" > "io/$PROBLEM.in"
awk '/^---OUTPUT---$/ {getline; while ($0 != "---END---") {print; getline}} END {}' "$TMPFILE" > "io/$PROBLEM.expected"
echo "Scraped problem $PROBLEM to io/$PROBLEM.in and io/$PROBLEM.expected"
else
echo "Failed to scrape problem $PROBLEM"
cat "$TMPFILE"
rm "$TMPFILE"
exit
fi
;;
atcoder)
if [ -z "$PROBLEM_LETTER" ]; then
echo "AtCoder requires problem letter (e.g., make scrape atcoder abc042 a)"
rm "$TMPFILE"
exit
fi
FULL_PROBLEM_ID="${PROBLEM}${PROBLEM_LETTER}"
cd "$(dirname "$0")/../.." && uv run scrapers/atcoder.py "$PROBLEM" "$PROBLEM_LETTER" > "$TMPFILE"
if [ $? -eq 0 ]; then
cd "$ORIGDIR"
awk '/^---INPUT---$/ {getline; while ($0 != "---OUTPUT---") {print; getline}} END {}' "$TMPFILE" > "io/$FULL_PROBLEM_ID.in"
awk '/^---OUTPUT---$/ {getline; while ($0 != "---END---") {print; getline}} END {}' "$TMPFILE" > "io/$FULL_PROBLEM_ID.expected"
echo "Scraped problem $FULL_PROBLEM_ID to io/$FULL_PROBLEM_ID.in and io/$FULL_PROBLEM_ID.expected"
else
echo "Failed to scrape problem $FULL_PROBLEM_ID"
cat "$TMPFILE"
rm "$TMPFILE"
exit
fi
;;
codeforces)
if [ -z "$PROBLEM_LETTER" ]; then
echo "Codeforces requires problem letter (e.g., make scrape codeforces 1234 A)"
rm "$TMPFILE"
exit
fi
FULL_PROBLEM_ID="${PROBLEM}${PROBLEM_LETTER}"
cd "$(dirname "$0")/../.." && uv run scrapers/codeforces.py "$PROBLEM" "$PROBLEM_LETTER" > "$TMPFILE"
if [ $? -eq 0 ]; then
cd "$ORIGDIR"
awk '/^---INPUT---$/ {getline; while ($0 != "---OUTPUT---") {print; getline}} END {}' "$TMPFILE" > "io/$FULL_PROBLEM_ID.in"
awk '/^---OUTPUT---$/ {getline; while ($0 != "---END---") {print; getline}} END {}' "$TMPFILE" > "io/$FULL_PROBLEM_ID.expected"
echo "Scraped problem $FULL_PROBLEM_ID to io/$FULL_PROBLEM_ID.in and io/$FULL_PROBLEM_ID.expected"
else
echo "Failed to scrape problem $FULL_PROBLEM_ID"
echo "You can manually add test cases to io/$FULL_PROBLEM_ID.in and io/$FULL_PROBLEM_ID.expected"
cat "$TMPFILE"
rm "$TMPFILE"
exit
fi
;;
*)
echo "Unknown contest type: $CONTEST"
echo "Available contests: cses, atcoder, codeforces"
rm "$TMPFILE"
exit
;;
esac
rm "$TMPFILE"

View file

@ -0,0 +1,73 @@
#!/bin/sh
execute_binary() {
binary="$1"
input="$2"
output="$3"
is_debug="$4"
start=$(date '+%s.%N')
if [ -n "$is_debug" ]; then
asan="$(ldconfig -p | grep libasan.so | head -n1 | awk '{print $4}')"
LD_PRELOAD="$asan" timeout 2s ./"$binary" <"$input" >"$output" 2>&1
else
timeout 2s ./"$binary" <"$input" >"$output" 2>&1
fi
CODE=$?
end=$(date '+%s.%N')
truncate -s "$(head -n 1000 "$output" | wc -c)" "$output"
if [ $CODE -ge 124 ]; then
MSG=''
case $CODE in
124) MSG='TIMEOUT' ;;
128) MSG='SIGILL' ;;
130) MSG='SIGABRT' ;;
131) MSG='SIGBUS' ;;
136) MSG='SIGFPE' ;;
135) MSG='SIGSEGV' ;;
137) MSG='SIGPIPE' ;;
139) MSG='SIGTERM' ;;
esac
[ $CODE -ne 124 ] && sed -i '$d' "$output"
test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output"
else
printf '\n[code]: %s' "$CODE" >>"$output"
fi
printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output
test -n "$is_debug" && is_debug_string=true || is_debug_string=false
printf '\n[debug]: %s' "$is_debug_string" >>$output
expected_file="${output%.out}.expected"
if [ -f "$expected_file" ] && [ $CODE -eq 0 ]; then
awk '/^\[[^]]*\]:/ {exit} {print}' "$output" > /tmp/program_output
if cmp -s /tmp/program_output "$expected_file"; then
printf '\n[matches]: true' >>"$output"
else
printf '\n[matches]: false' >>"$output"
fi
rm -f /tmp/program_output
fi
return $CODE
}
compile_source() {
src="$1"
bin="$2"
output="$3"
flags="$4"
test -f "$bin" && rm "$bin" || true
g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output"
CODE=$?
if [ $CODE -gt 0 ]; then
printf '\n[code]: %s' "$CODE" >>"$output"
return $CODE
else
echo '' >"$output"
return 0
fi
}

View file

@ -0,0 +1,9 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}