Skip to content

Commit

Permalink
Merge pull request #173 from Stegallo/2015
Browse files Browse the repository at this point in the history
2015
  • Loading branch information
Stegallo authored Nov 5, 2023
2 parents bdf6601 + 3a524cf commit cbc4dcb
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions y_2015/day3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic.dataclasses import dataclass

from typing import List, Set
from common.aoc import AoCDay


Expand All @@ -21,47 +21,43 @@ def hash(self) -> str:
}


@dataclass
class Santa:
sequence: str
current_position = Position(0, 0)

def set_grid(self, grid) -> None:
self.grid = grid

def deliver_presents(self) -> None:
for i in self.sequence:
self.current_position = Position(
self.current_position.x + OPERATIONS[i].x,
self.current_position.y + OPERATIONS[i].y,
)
def __init__(self, grid) -> None:
self.grid: Set = grid

self.grid.add(self.current_position.hash)
def deliver_present(self, command) -> None:
self.current_position = Position(
self.current_position.x + OPERATIONS[command].x,
self.current_position.y + OPERATIONS[command].y,
)
self.grid.add(self.current_position.hash)


class Day(AoCDay):
def __init__(self, test=0):
super().__init__(__name__, test)

def _preprocess_input(self) -> None:
self.__santa = Santa(self._input_data[0][0])
self.__real_santa = Santa(
"".join([i for c, i in enumerate(self._input_data[0][0]) if c % 2 == 0])
)
self.__robo_santa = Santa(
"".join([i for c, i in enumerate(self._input_data[0][0]) if c % 2 != 0])
)
self.__sequence = self._input_data[0][0]

def initialize(self) -> None:
self.grid: Set = {Position(0, 0).hash}

def dispatch(self, actors: List[str]) -> int:
santas = [Santa(self.grid) for _ in actors]
active_santa = 0
for command in self.__sequence:
santas[active_santa].deliver_present(command)
active_santa = (active_santa + 1) % len(santas)

return len(self.grid)

def _calculate_1(self) -> int:
grid = {Position(0, 0).hash}
self.__santa.set_grid(grid)
self.__santa.deliver_presents()
return len(self._Day__santa.grid)
self.initialize()
return self.dispatch(["Santa"])

def _calculate_2(self) -> int:
grid = {Position(0, 0).hash}
self.__real_santa.set_grid(grid)
self.__robo_santa.set_grid(grid)
self.__real_santa.deliver_presents()
self.__robo_santa.deliver_presents()
return len(grid)
self.initialize()
return self.dispatch(["Santa", "RoboSanta"])

0 comments on commit cbc4dcb

Please sign in to comment.