1
0

aocutils.py 3.2 KB

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