|
|
@@ -0,0 +1,75 @@ |
|
|
|
import enum |
|
|
|
import logging |
|
|
|
|
|
|
|
from sys import stdout |
|
|
|
from typing import List, Tuple |
|
|
|
|
|
|
|
logger = logging.Logger(__name__) |
|
|
|
logger_2 = logging.Logger(f"{__name__}_2") |
|
|
|
formatter = logging.Formatter('[%(asctime)s][%(levelname)s] %(message)s') |
|
|
|
sh = logging.StreamHandler(stdout) |
|
|
|
sh.setLevel(logging.INFO) |
|
|
|
sh.setFormatter(formatter) |
|
|
|
fh = logging.FileHandler("./day02-2.log", mode="w", encoding="utf-8") |
|
|
|
fh_2 = logging.FileHandler("./day02-2_round2.log", mode="w", encoding="utf-8") |
|
|
|
fh.setLevel(logging.DEBUG) |
|
|
|
fh.setFormatter(formatter) |
|
|
|
fh_2.setLevel(logging.DEBUG) |
|
|
|
fh_2.setFormatter(formatter) |
|
|
|
logger.addHandler(sh) |
|
|
|
logger.addHandler(fh) |
|
|
|
logger_2.addHandler(fh_2) |
|
|
|
|
|
|
|
class Direction(enum.Enum): |
|
|
|
UP = 0 |
|
|
|
RIGHT = 1 |
|
|
|
DOWN = 2 |
|
|
|
LEFT = 3 |
|
|
|
|
|
|
|
class Guard: |
|
|
|
def __init__(self, initial_x: int, initial_y: int, initial_dir: int) -> object: |
|
|
|
self.x = self.initial_x = initial_x |
|
|
|
self.y = self.initial_y = initial_y |
|
|
|
self.direction = self.initial_dir = initial_dir |
|
|
|
|
|
|
|
@property |
|
|
|
def dir(self): |
|
|
|
return self.direction |
|
|
|
|
|
|
|
@property.setter |
|
|
|
def dir(self, new_dir: Direction) -> None: |
|
|
|
self.direction = new_dir |
|
|
|
|
|
|
|
def find_guard(grid: List[List[str]], gchar: str) -> Tuple[int,int]: |
|
|
|
breakout = False |
|
|
|
x,y,dir = None,None,None |
|
|
|
for j, line in grid: |
|
|
|
if breakout: |
|
|
|
break |
|
|
|
for i, char in line: |
|
|
|
if char in ["^",">","<","v"]: |
|
|
|
x, y = i, j |
|
|
|
match char: |
|
|
|
case "^": |
|
|
|
dir = Direction.UP |
|
|
|
case ">": |
|
|
|
dir = Direction.RIGHT |
|
|
|
case "v": |
|
|
|
dir = Direction.DOWN |
|
|
|
case "<": |
|
|
|
dir = Direction.LEFT |
|
|
|
case "_": |
|
|
|
raise ValueError(f"char must be one of '^','>','v','<', received {char}") |
|
|
|
breakout = True |
|
|
|
break |
|
|
|
return (x,y,dir) |
|
|
|
|
|
|
|
def main061(): |
|
|
|
with open("input06.txt", "r", encoding="utf-8") as f: |
|
|
|
grid = [list(l.split(" ")) for l in f.readlines()] |
|
|
|
|
|
|
|
guard_position = find_guard(grid) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
main061() |