aocutils.py 2.8 KB

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