1
0

aocutils.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import os
  2. import requests
  3. import time
  4. import bs4
  5. import markdownify as md
  6. YEAR = "2025"
  7. session_id = "53616c7465645f5f74bf2aff62945c49fba5f96848423a482d4c5b51ac366cb778fe7aaa623cedb5d38ff64f30317c6146590748dab77ccd9e902633765a29ce"
  8. def create_files(task_dir: str, day: int):
  9. go_path = os.path.join(task_dir, f"day{day}.go")
  10. python_path = os.path.join(task_dir, f"day{day}.py")
  11. if os.path.exists(go_path):
  12. os.utime(go_path, None)
  13. else:
  14. open(go_path, "a").close()
  15. if os.path.exists(python_path):
  16. os.utime(python_path, None)
  17. else:
  18. open(python_path, "a").close()
  19. def generate_readme(task_dir: str, day: int):
  20. os.makedirs(task_dir, exist_ok=True)
  21. readme_path = os.path.join(task_dir, "README.md")
  22. cookies_dict = {"session": session_id}
  23. soup = bs4.BeautifulSoup(
  24. requests.get(f"https://adventofcode.com/{YEAR}/day/{day}", cookies=cookies_dict).content,
  25. features="html.parser",
  26. )
  27. with open(readme_path, "w") as readme:
  28. readme.write(md.markdownify(str(soup.find_all("article")[0])))
  29. if len(soup.find_all("article")) > 1:
  30. with open(readme_path, "a") as readme:
  31. readme.write(md.markdownify(str(soup.find_all("article")[1])))
  32. def get_input(task_dir: str, day: int) -> tuple[list[str], list[str]] | None:
  33. input_path = os.path.join(task_dir, "input.txt")
  34. example_path = os.path.join(task_dir, "example.txt")
  35. readme_path = os.path.join(task_dir, "README.md")
  36. cookies_dict = {"session": session_id}
  37. os.makedirs(task_dir, exist_ok=True)
  38. if os.path.exists(input_path):
  39. with open(input_path, "r") as f:
  40. input = f.read().splitlines()
  41. else:
  42. input = requests.get(f"https://adventofcode.com/{YEAR}/day/{day}/input", cookies=cookies_dict).text
  43. with open(input_path, "w") as f:
  44. f.write(input.strip())
  45. input = input.splitlines()
  46. if os.path.exists(example_path):
  47. with open(example_path, "r") as e:
  48. example = e.read().splitlines()
  49. elif os.path.exists(readme_path):
  50. with open(example_path, "w") as e:
  51. with open(readme_path, "r") as r:
  52. example = r.read().split("\n\n```\n")[1]
  53. e.write(example)
  54. example = example.splitlines()
  55. else:
  56. print("call `generate_readme()` first!")
  57. return
  58. return input, example
  59. def bench(part):
  60. def wrapper(*args, **kwargs):
  61. start = time.perf_counter()
  62. value = part(*args, **kwargs)
  63. print(f"\tevaluation time: {time.perf_counter() - start} s")
  64. return value
  65. return wrapper
  66. if __name__ == "__main__":
  67. day = 1
  68. root = os.path.dirname(__file__)
  69. task_dir = os.path.join(root, f"day{day}")
  70. generate_readme(task_dir, day)
  71. get_input(task_dir, day)
  72. create_files(task_dir, day)