get_tasks.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import os
  2. import requests
  3. import bs4
  4. import markdownify as md
  5. import time
  6. from typing import Any, Callable
  7. def generate_readme(task_dir: str, day: int):
  8. readme_path = os.path.join(task_dir, "README.md")
  9. cookies_dict = {
  10. "session": "53616c7465645f5ffe3db8d154199da4d6e4e569142fda21d3350f5e550f2a4c509bd1b147264ffe0a0d2124909ec5d6"
  11. }
  12. if os.path.exists(readme_path):
  13. pass
  14. else:
  15. soup = bs4.BeautifulSoup(
  16. requests.get(
  17. f"https://adventofcode.com/2021/day/{day}", cookies=cookies_dict
  18. ).content,
  19. features="html.parser",
  20. )
  21. with open(readme_path, "w") as readme:
  22. readme.write(md.markdownify(str(soup.find_all("article")[0])))
  23. if len(soup.find_all("article")) > 1:
  24. with open(readme_path, "a") as readme:
  25. readme.write(md.markdownify(str(soup.find_all("article")[1])))
  26. def get_input(task_dir: str, day: int) -> tuple[list[str], list[str]]:
  27. input_path = os.path.join(task_dir, "input.txt")
  28. example_path = os.path.join(task_dir, "example.txt")
  29. cookies_dict = {
  30. "session": "53616c7465645f5ffe3db8d154199da4d6e4e569142fda21d3350f5e550f2a4c509bd1b147264ffe0a0d2124909ec5d6"
  31. }
  32. os.makedirs(task_dir, exist_ok=True)
  33. if os.path.exists(input_path):
  34. with open(input_path, "r") as f:
  35. input = f.read().splitlines()
  36. else:
  37. input = requests.get(
  38. f"https://adventofcode.com/2021/day/{day}/input", cookies=cookies_dict
  39. ).text
  40. with open(input_path, "w") as f:
  41. f.write(input)
  42. input = input.splitlines()
  43. if os.path.exists(example_path):
  44. with open(example_path, "r") as e:
  45. example = e.read().splitlines()
  46. else:
  47. example = bs4.BeautifulSoup(
  48. requests.get(
  49. f"https://adventofcode.com/2021/day/{day}", cookies=cookies_dict
  50. ).content,
  51. features="html.parser",
  52. ).code.text
  53. with open(example_path, "w") as f:
  54. f.write(example)
  55. example = example.splitlines()
  56. return input, example
  57. def bench(part):
  58. def wrapper(*args, **kwargs):
  59. start = time.perf_counter()
  60. value = part(*args, **kwargs)
  61. print(f"\tevaluation time: {time.perf_counter() - start} s")
  62. return value
  63. return wrapper
  64. def check_example(example: Any, part: Callable):
  65. print(f'\n{10*"-"}Example test here{10*"-"}\n')
  66. part(example)
  67. print(f'\n{10*"-"}End example test{10*"-"}\n')
  68. if __name__ == "__main__":
  69. # print(get_input("day_1_sonar_sweep", 3))
  70. generate_readme("../day3_binary_diagnostic", 3)