[Advent of Code 2024](https://adventofcode.com/2024)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

day06-1.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import enum
  2. import logging
  3. from sys import stdout
  4. from typing import List, Tuple
  5. logger = logging.Logger(__name__)
  6. logger_2 = logging.Logger(f"{__name__}_2")
  7. formatter = logging.Formatter('[%(asctime)s][%(levelname)s] %(message)s')
  8. sh = logging.StreamHandler(stdout)
  9. sh.setLevel(logging.INFO)
  10. sh.setFormatter(formatter)
  11. fh = logging.FileHandler("./day02-2.log", mode="w", encoding="utf-8")
  12. fh_2 = logging.FileHandler("./day02-2_round2.log", mode="w", encoding="utf-8")
  13. fh.setLevel(logging.DEBUG)
  14. fh.setFormatter(formatter)
  15. fh_2.setLevel(logging.DEBUG)
  16. fh_2.setFormatter(formatter)
  17. logger.addHandler(sh)
  18. logger.addHandler(fh)
  19. logger_2.addHandler(fh_2)
  20. class Direction(enum.Enum):
  21. UP = 0
  22. RIGHT = 1
  23. DOWN = 2
  24. LEFT = 3
  25. class Guard:
  26. def __init__(self, initial_x: int, initial_y: int, initial_dir: int) -> object:
  27. self.x = self.initial_x = initial_x
  28. self.y = self.initial_y = initial_y
  29. self.direction = self.initial_dir = initial_dir
  30. @property
  31. def dir(self):
  32. return self.direction
  33. @property.setter
  34. def dir(self, new_dir: Direction) -> None:
  35. self.direction = new_dir
  36. def find_guard(grid: List[List[str]], gchar: str) -> Tuple[int,int]:
  37. breakout = False
  38. x,y,dir = None,None,None
  39. for j, line in grid:
  40. if breakout:
  41. break
  42. for i, char in line:
  43. if char in ["^",">","<","v"]:
  44. x, y = i, j
  45. match char:
  46. case "^":
  47. dir = Direction.UP
  48. case ">":
  49. dir = Direction.RIGHT
  50. case "v":
  51. dir = Direction.DOWN
  52. case "<":
  53. dir = Direction.LEFT
  54. case "_":
  55. raise ValueError(f"char must be one of '^','>','v','<', received {char}")
  56. breakout = True
  57. break
  58. return (x,y,dir)
  59. def main061():
  60. with open("input06.txt", "r", encoding="utf-8") as f:
  61. grid = [list(l.split(" ")) for l in f.readlines()]
  62. guard_position = find_guard(grid)
  63. if __name__ == "__main__":
  64. main061()