Skip to content

Commit

Permalink
Merge pull request #6 from Vasile-Hij/refactor-day-01-07-no-05
Browse files Browse the repository at this point in the history
refactor-day-01-07-no-05
  • Loading branch information
Vasile-Hij authored May 25, 2023
2 parents d655c37 + f81e825 commit f46e85e
Show file tree
Hide file tree
Showing 15 changed files with 589 additions and 784 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,5 @@ dmypy.json
.pyre/

.idea
.vscode
.vscode
http_eg
50 changes: 40 additions & 10 deletions common/util.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import re
from collections import Counter

separator = '─'*100
lines = str.splitlines


def paragraph(text):
return text.split('\n\n')
separator = '─' * 100
lines = str.splitlines


def helper_base(source: str, functions: callable = str, display: int = 10):
def helper_base(source: str, year: str, functions: callable = str, display: int = 10):
day_text = read_raw(source)
name, function, segmentation = functions()

parser_function = get_function(function)

if segmentation != 'lines':
custom_segmentation = get_function(segmentation)
segmentation = custom_segmentation(day_text.rstrip())
else:
segmentation = lines(day_text.rstrip())
print(name)

_year = ''.join(['--- Year: 20', year])
print(_year, name)

display_items('Raw input', day_text.splitlines(), display)
data = make_tuple(parser_function, segmentation)
if parser_function != str or parser_function != lines:
Expand All @@ -40,6 +39,12 @@ def get_function(function: str):
return method


def get_functions(source, *args):
if get_function(args):
return True
return False


def display_items(file_raw, items, display: int, separate=separator):
if display:
type_input = Counter(map(type, items))
Expand Down Expand Up @@ -72,14 +77,14 @@ def printer(part: str, result: str, func: callable = str, separate: str = separa
_part = 'Part 2'

results = f'{_part}: {func(result)}'
return f'{separate}\n{results}\n{separate}'
print(f'{separate}\n{results}\n{separate}')


def truncate(obj, width: int = 100, ellipsis: str = ' ...'):
string = str(obj)
if len(string) <= width:
return string
return string[:width - len(ellipsis)] + ellipsis
return string[: width - len(ellipsis)] + ellipsis


def make_tuple(function: callable, *sequences):
Expand All @@ -90,8 +95,33 @@ def make_list(function: callable, *sequences):
return list(map(function, *sequences))


def str_strip(data: str):
return data.strip()


def str_split(data: str):
return data.split()


def paragraph(data: str):
return data.split('\n\n')


def integers(data: str):
return make_tuple(int, re.findall(r'-?[0-9]+', data))


def find_digits(text: str):
return make_tuple(int, re.findall(r'[0-9]', text))


four_directions = ((1, 0), (0, 1), (-1, 0), (0, -1))


def each_first_item(data: str):
return [item[0] for item in data]


def each_item(data: str):
return [item for item in data]

25 changes: 9 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import argparse
import sys
from common.checker import clean_input
from common.settings import files
from common.util import helper_base, printer
from importlib import import_module


def run(func, part, source, functions):
result = helper_base(source, functions)
print(printer(part, result, func))


if __name__ == '__main__':
_help = \
"""
_help = """
1. For year 2022 and day 01 type: -v 2201 ("-v" is value, "22" is directory , "01" is "day01")
2. If you type only 2 digits, it will be considered that day, but no more than 25 (if that day is solved yet),
and year it will be the latest year available in 'input' directory.
Expand All @@ -35,16 +30,14 @@ def run(func, part, source, functions):
input_path = files['input_path'].format(year=year, day=day, sample='')

script = import_module(script_path)
print(f'year: {year}, day: {day}')

for each_day in ("part_1", "part_2"):
if not hasattr(script, 'start_day'):
print('Please define input function and/or separator')
continue
if not all(hasattr(script, checker) for checker in ['start_day', 'helper', 'part_1', 'part_2']):
print(f'Please define all functions as in "blank.py" template')
sys.exit()

functions_required = getattr(script, 'start_day')

if not hasattr(script, each_day):
continue
run(getattr(script, each_day), each_day, input_path, functions_required)
functions = getattr(script, 'start_day')
result = helper_base(source=input_path, year=year, functions=functions, )

for each_day in ["part_1", "part_2"]:
printer(part=each_day, result=result, func=getattr(script, each_day))
Loading

0 comments on commit f46e85e

Please sign in to comment.