From fae76cd84b7467367e66e7a10a76a6715e958daf Mon Sep 17 00:00:00 2001 From: racoelhosilva Date: Mon, 4 Mar 2024 13:40:06 +0000 Subject: [PATCH] Initital AdventOfCode files --- aoc-ar | 44 ++++++++++++++++++++++++++++++ aoc-new | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100755 aoc-ar create mode 100755 aoc-new diff --git a/aoc-ar b/aoc-ar new file mode 100755 index 0000000..111e2b4 --- /dev/null +++ b/aoc-ar @@ -0,0 +1,44 @@ +#!/usr/bin/python3 +import sys, os, subprocess + +def adventOfCode(year, day): + + currentDir = os.getcwd() + commands = [] + # Check if year directory exists + yearDir = os.path.join(currentDir, year) + if not os.path.exists(yearDir): + print("Error: Year Directory not found!") + exit(1) + + # Check if day directory exists + dayDir = os.path.join(yearDir, f"day{int(day):02d}") + if not os.path.exists(dayDir): + print("Error: Day Directory not found!") + exit(1) + + # Check if solution file exists + solutionPath = os.path.join(dayDir, "main.py") + if not os.path.exists(solutionPath): + print("Error: Solution File not found!") + exit(1) + + commands.append(f"git add {solutionPath}") + + # Add input file if exists + inputPath = os.path.join(dayDir, "input.txt") + if os.path.exists(inputPath): + commands.append(f"git add {inputPath}") + + # Get problem title from file + with open(solutionPath, "r") as solutionFile: + title = solutionFile.read().splitlines()[1].split(":")[1][1:] + + commands.append(f"git commit -m \"{year} Day {int(day):02d}: {title}\"") + + for command in commands: + subprocess.run(command, shell=True, check=True) + +if __name__ == "__main__": + if (len(sys.argv) == 3): + adventOfCode(sys.argv[1], sys.argv[2]) \ No newline at end of file diff --git a/aoc-new b/aoc-new new file mode 100755 index 0000000..7b97b2b --- /dev/null +++ b/aoc-new @@ -0,0 +1,83 @@ +#!/usr/bin/python3 +import sys, os, requests, html, re + +def getPuzzleInput(year, day): + + session_token = os.environ.get("AOC_COOKIE") + headers = {'Cookie': f'session={session_token}'} + request = requests.get(f'https://adventofcode.com/{year}/day/{day}/input', headers=headers) + + if request.status_code == 200: + with open(os.path.join(os.getcwd(), year, f"day{int(day):02d}", "input.txt"), "w") as inputText: + inputText.write(request.text.strip()) + elif request.status_code == 404: + print(f"Error: Puzzle for day not found.") + elif request.status_code == 403: + print("Error: Authentication failed. Make sure to provide a valid session token.") + else: + print(f"Error: Failed to fetch puzzle input. Status code: {request.status_code}") + +def getPuzzleName(year, day): + request = requests.get(f'https://adventofcode.com/{year}/day/{day}') + + if request.status_code == 404: + print(f"Error: Puzzle for day not found.") + return + + request.encoding = 'utf-8' + html_doc = html.unescape(request.text) + title = re.findall(r"--- (.*?) ---", html_doc, re.DOTALL)[0].split(":")[1][1:] + return f"Day {int(day):02d}: {title}" + +def adventOfCode(year, day): + + currentDir = os.getcwd() + + # Check if year directory exists and set it up + yearDir = os.path.join(currentDir, year) + if not os.path.exists(yearDir): + os.mkdir(yearDir) + + # Check if day directory exists and set it up + dayDir = os.path.join(yearDir, f"day{int(day):02d}") + if not os.path.exists(dayDir): + os.mkdir(dayDir) + + # Get puzzle name + puzzleName = getPuzzleName(year, day) + + # Create solution file (by default in python) + solutionPath = os.path.join(dayDir, "main.py") + if not os.path.exists(solutionPath): + with open(solutionPath, "w") as solutionFile: + solutionFile.write( + f"""# Advent of Code {year} +# {puzzleName} +# https://adventofcode.com/{year}/day/{day} + +def a(data): + return + +def b(data): + return + +def test(): + print() + print("Test 1:", "Passed" if a([]) == None else "Failed") + print("Test 2:", "Passed" if b([]) == None else "Failed") + print() + +if __name__ == \"__main__\": + print(\"\\tAdvent of Code {year}\\n{puzzleName}\") + data = open(\"{os.path.join(dayDir, 'input.txt')}\", \"r\").read().splitlines() + test() + print(\"Part a:\", a(data)) + print(\"Part b:\", b(data))""") + + # Create input file + getPuzzleInput(year, day) + + +if __name__ == "__main__": + if (len(sys.argv) == 3): + adventOfCode(sys.argv[1], sys.argv[2]) \ No newline at end of file