aocutils.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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("\n\n")
  40. readme.write(md.markdownify(str(soup.find_all("article")[1])))
  41. def get_input(task_dir: str, day: int) -> tuple[list[str], list[str]] | None:
  42. input_path = os.path.join(task_dir, "input.txt")
  43. example_path = os.path.join(task_dir, "example.txt")
  44. readme_path = os.path.join(task_dir, "README.md")
  45. cookies_dict = {"session": session_id}
  46. os.makedirs(task_dir, exist_ok=True)
  47. if os.path.exists(input_path):
  48. with open(input_path, "r") as f:
  49. input = f.read().splitlines()
  50. else:
  51. input = requests.get(f"https://adventofcode.com/{YEAR}/day/{day}/input", cookies=cookies_dict).text
  52. with open(input_path, "w") as f:
  53. f.write(input.strip())
  54. input = input.splitlines()
  55. if os.path.exists(example_path):
  56. with open(example_path, "r") as e:
  57. example = e.read().splitlines()
  58. elif os.path.exists(readme_path):
  59. with open(example_path, "w") as e:
  60. with open(readme_path, "r") as r:
  61. example = r.read().split("\n\n```\n")[1]
  62. e.write(example)
  63. example = example.splitlines()
  64. else:
  65. print("call `generate_readme()` first!")
  66. return
  67. return input, example
  68. def bench(part):
  69. def wrapper(*args, **kwargs):
  70. start = time.perf_counter()
  71. value = part(*args, **kwargs)
  72. print(f"\tevaluation time: {time.perf_counter() - start} s")
  73. return value
  74. return wrapper
  75. if __name__ == "__main__":
  76. day = 6
  77. root = os.path.dirname(__file__)
  78. task_dir = os.path.join(root, f"day{day}")
  79. generate_readme(task_dir, day)
  80. get_input(task_dir, day)
  81. create_files(task_dir, day)