-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from Stegallo/restructure
Restructure
- Loading branch information
Showing
125 changed files
with
265 additions
and
5,957 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import argparse | ||
import importlib | ||
from abc import ABC, abstractmethod | ||
from datetime import datetime | ||
from typing import List | ||
|
||
from .get_file import pull_file | ||
|
||
|
||
class AoCDay(ABC): | ||
""" | ||
Abstract class Day | ||
""" | ||
|
||
@abstractmethod | ||
def __init__(self, path: str, test: bool): | ||
day_info = AoCDay.get_day_info(path) | ||
self._year = day_info.year | ||
self._day = day_info.day | ||
self._test = test | ||
self._input_data = self._load_input() | ||
self._preprocess_input() | ||
self.__stop_watch = StopWatch() | ||
|
||
@abstractmethod | ||
def _preprocess_input(self): | ||
""" | ||
preprocessing of the input | ||
""" | ||
|
||
@abstractmethod | ||
def _calculate_1(self): | ||
""" | ||
_calculate_1 | ||
""" | ||
|
||
@abstractmethod | ||
def _calculate_2(self): | ||
""" | ||
_calculate_2 | ||
""" | ||
|
||
@staticmethod | ||
def get_day_info(path: str): | ||
""" | ||
TODO typehint | ||
""" | ||
return DayInfo.from_path(path) | ||
|
||
def _load_input(self) -> List[List[str]]: | ||
raw_string = self._get_file_content() | ||
|
||
if raw_string and raw_string[-1] == "\n": | ||
raw_string = raw_string[:-1] | ||
chunks = raw_string.split("\n\n") | ||
|
||
return [k.split("\n") for k in chunks] | ||
|
||
def _get_file_content(self): | ||
file_name = ( | ||
f"y_{self._year}/input_day{self._day}{'_test'if self._test else ''}.txt" | ||
) | ||
try: | ||
with open(file_name) as f: | ||
raw_string = f.read() | ||
except FileNotFoundError: | ||
pull_file(self._year, self._day) | ||
with open(file_name) as f: | ||
raw_string = f.read() | ||
return raw_string | ||
|
||
def solve(self): | ||
self.__stop_watch.start() | ||
print(f"sol 1: {self._calculate_1()} Time taken: {self.__stop_watch.lap()}") | ||
|
||
print(f"sol 2: {self._calculate_2()} Time taken: {self.__stop_watch.stop()}") | ||
|
||
|
||
class DayInfo: | ||
year: int | ||
day: int | ||
|
||
@staticmethod | ||
def from_path(path: str): | ||
""" | ||
TODO typehint | ||
""" | ||
day_info = DayInfo() | ||
day_info.year = path.split(".")[0].replace("y_", "") | ||
day_info.day = path.split(".")[1].replace("day", "") | ||
return day_info | ||
|
||
|
||
class StopWatch: | ||
def __init__(self): | ||
self.__start_time = None | ||
|
||
def start(self): | ||
self.__start_time = datetime.now() | ||
|
||
def lap(self): | ||
return datetime.now() - self.__start_time | ||
|
||
def stop(self): | ||
return datetime.now() - self.__start_time | ||
|
||
|
||
def main(): | ||
"""""" | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--year", type=int, help="year as a number") | ||
parser.add_argument("--day", type=int, help="day as a number") | ||
parser.add_argument("--test", type=int, help="test flag") | ||
args = parser.parse_args() | ||
|
||
module = importlib.import_module(f"y_{args.year}.day{args.day}") | ||
|
||
day = getattr(module, "Day")(test=args.test) | ||
day.solve() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pytest | ||
isort | ||
black | ||
flake8 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .common import main | ||
from common.aoc import main | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import contextlib | ||
from unittest.mock import patch | ||
|
||
from common.aoc import AoCDay, main | ||
|
||
|
||
def test_load_input_string(): | ||
with patch("common.aoc.open") as open_patch: | ||
AoCDay.__abstractmethods__ = set() | ||
aoc_day = AoCDay("y_1900.day1", False) | ||
|
||
open_patch().__enter__().read = lambda: "hello" | ||
assert aoc_day._load_input() == [["hello"]] | ||
open_patch().__enter__().read = lambda: "" | ||
assert aoc_day._load_input() == [[""]] | ||
|
||
|
||
def test_load_input_multiline(): | ||
with patch("common.aoc.open") as open_patch: | ||
AoCDay.__abstractmethods__ = set() | ||
aoc_day = AoCDay("y_1900.day1", False) | ||
|
||
open_patch().__enter__().read = lambda: "hello\nworld\n" | ||
assert aoc_day._load_input() == [["hello", "world"]] | ||
|
||
|
||
def test_load_input_multiline_multisection(): | ||
with patch("common.aoc.open") as open_patch: | ||
AoCDay.__abstractmethods__ = set() | ||
aoc_day = AoCDay("y_1900.day1", False) | ||
|
||
open_patch().__enter__().read = ( | ||
lambda: "hello\nworld\n\ngoodbye\nworld\n\nbye\n" | ||
) | ||
assert aoc_day._load_input() == [ | ||
["hello", "world"], | ||
["goodbye", "world"], | ||
["bye"], | ||
] | ||
|
||
|
||
def test_main(): | ||
with contextlib.ExitStack() as patches: | ||
patches.enter_context(patch("common.aoc.argparse")) | ||
patches.enter_context(patch("common.aoc.open")) | ||
patches.enter_context(patch("common.aoc.importlib")) | ||
assert main() is None | ||
|
||
|
||
def test_AoCDay(): | ||
with patch("common.aoc.open") as open_patch: | ||
AoCDay.__abstractmethods__ = set() | ||
aoc_day = AoCDay("y_1900.day1", False) | ||
open_patch().__enter__().read = lambda: "hello\nworld\n" | ||
assert aoc_day.solve() is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,45 @@ | ||
from y_2015.day1 import calculate_1, calculate_2 | ||
from __future__ import annotations | ||
|
||
from unittest.mock import mock_open, patch | ||
|
||
from y_2015.day1 import Day | ||
|
||
with patch("builtins.open", mock_open(read_data="0")): | ||
day = Day() | ||
|
||
|
||
def test_calculate_1(): | ||
assert calculate_1("(())") == 0 | ||
assert calculate_1("()()") == 0 | ||
assert calculate_1("(((") == 3 | ||
assert calculate_1("(()(()(") == 3 | ||
assert calculate_1("))(((((") == 3 | ||
assert calculate_1("())") == -1 | ||
assert calculate_1("))(") == -1 | ||
assert calculate_1(")))") == -3 | ||
assert calculate_1(")())())") == -3 | ||
day._Day__input_data = "(())" | ||
assert day._calculate_1() == 0 | ||
|
||
day._Day__input_data = "(((" | ||
assert day._calculate_1() == 3 | ||
|
||
day._Day__input_data = "(()(()(" | ||
assert day._calculate_1() == 3 | ||
|
||
day._Day__input_data = "))(((((" | ||
assert day._calculate_1() == 3 | ||
|
||
day._Day__input_data = "())" | ||
assert day._calculate_1() == -1 | ||
|
||
day._Day__input_data = "))(" | ||
assert day._calculate_1() == -1 | ||
|
||
day._Day__input_data = ")))" | ||
assert day._calculate_1() == -3 | ||
|
||
day._Day__input_data = ")())())" | ||
assert day._calculate_1() == -3 | ||
|
||
|
||
def test_calculate_2(): | ||
assert calculate_2(")") == 1 | ||
assert calculate_2("()())") == 5 | ||
assert calculate_2("(") == -1 | ||
day._Day__input_data = ")" | ||
assert day._calculate_2() == 1 | ||
|
||
day._Day__input_data = "()())" | ||
assert day._calculate_2() == 5 | ||
|
||
day._Day__input_data = "(" | ||
assert day._calculate_2() == -1 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.