Skip to content

Commit

Permalink
Merge remote-tracking branch 'advent_of_code_antoine_ganne/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
#	requirements_dev.txt
  • Loading branch information
Veganaise committed Dec 3, 2024
2 parents 06eb346 + e701514 commit 6ccbce1
Show file tree
Hide file tree
Showing 32 changed files with 112 additions and 248 deletions.
File renamed without changes.
12 changes: 8 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
rich==10.14.0
rich==13.4.2

requests~=2.26.0
pytest~=6.2.5
setuptools~=59.4.0
requests==2.31.0
pytest==7.4.0
setuptools==68.0.0
numpy==1.25.1
networkx==3.1
z3==0.2.0
z3-solver==4.12.2.0
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = adventofcode
description = Collection of Advent of Code solutions
author = Marcel Blijleven, Antoine Ganne
license = MIT
license_file = LICENSE
license_files = LICENSE
platforms = unix, linux, osx
classifiers =
Programming Language :: Python :: 3
Expand Down
100 changes: 0 additions & 100 deletions src/adventofcode/inputs/2019/day_01.txt

This file was deleted.

83 changes: 31 additions & 52 deletions src/adventofcode/scripts/add_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from typing import List, Tuple
from datetime import date

from requests import HTTPError # noqa
from requests import HTTPError

from adventofcode.config import ROOT_DIR
from adventofcode.scripts.get_inputs import get_input
from adventofcode.util.console import console
from adventofcode.util.input_helpers import get_input_for_day
from adventofcode.util.input_helpers import get_input_for_day, get_directory_path, get_input_directory_path, \
get_example_input_path, get_input_path


def add_day():
Expand All @@ -18,43 +19,41 @@ def add_day():
year = "2022"
console.print(f'Creating solution day file for year {year} day {day}')

# Solution file
module_path = os.path.join(ROOT_DIR, f'year_{year}')
solution_file = os.path.join(module_path, f'day_{day:02}_{year}.py')
create_module_dir(module_path)
write_solution_template(solution_file, year, day)

# Test file
test_module_path = os.path.abspath(os.path.join(ROOT_DIR, '../../tests', f'year_{year}'))
test_file = os.path.join(test_module_path, f'test_day_{day:02}_{year}.py')
create_module_dir(test_module_path)
write_test_template(test_file, year, day)
day_directory_path = get_directory_path(year, day)
create_module_dir(day_directory_path)

# Empty test input
test_input_module_path = os.path.abspath(os.path.join(ROOT_DIR, '../../tests', f'year_{year}', f'inputs'))
test_file_input = os.path.join(test_input_module_path, f'day_{day:02}.txt')
create_dir(test_input_module_path)
write_template(test_file_input, "")
solution_file = os.path.join(day_directory_path, f'solution_{day:02}_{year:4}.py')
solution_template_path = os.path.join(ROOT_DIR, 'scripts', 'templates', 'day_template.txt')
write_solution_template(solution_file, solution_template_path, year, day)

test_file = os.path.join(day_directory_path, f'test_day_{day:02}_{year}.py')
test_template_path = os.path.join(ROOT_DIR, 'scripts', 'templates', 'test_template.txt')
write_solution_template(test_file, test_template_path, year, day)

input_module_path = get_input_directory_path(year, day)
create_dir(input_module_path)

example_file_input = get_example_input_path(year, day)
if not os.path.exists(example_file_input):
with open(example_file_input, 'w'):
pass

verify_input_exists(year, day)


def write_solution_template(path: str, year: int, day: int) -> None:
def write_solution_template(path: str, template_path: str, year: int, day: int) -> None:
if not os.path.exists(path):
write_template(path, read_solution_template(year, day))
template = _read_template(template_path, year, day)
with open(path, 'w') as f:
f.write(template)
console.print(f'[green]Wrote template to {path}')
else:
console.print(f'[yellow]Did not write template for year {year} day {day}, the file already exists.')


def write_test_template(path: str, year: int, day: int) -> None:
if not os.path.exists(path):
write_template(path, read_test_template(year, day))
console.print(f'[green]Wrote test template to {path}')
else:
console.print(f'[yellow]Did not write test template for year {year} day {day}, the file already exists.')


def create_module_dir(path: str) -> None:
create_dir(path)

Expand All @@ -81,45 +80,25 @@ def verify_input_exists(year: int, day: int) -> None:
except HTTPError as e:
console.print(f'[red]Could not retrieve input data for year {year} day {day} automatically: {e}')
except FileNotFoundError:
console.print(f'[red]Could not retrieve input data for year {year} day {day}: .session not set correctly')
console.print(f'[red]Could not retrieve input data for year {year} day {day}: .session set incorrectly')

raise ValueError('unknown exception occurred in verify_input_exists')
raise ValueError('Exception occurred in verify_input_exists')


def _read_solution_template(template_path: str, year: str, day: str) -> str:
def _read_template(template_path: str, year: int, day: int) -> str:
with open(template_path) as f:
template = f.read()

template = template.replace('{year}', year)
template = template.replace('{day}', day)

return template


def _read_test_template(template_path: str, year: str, day: str, file_day: str) -> str:
with open(template_path) as f:
template = f.read()

template = template.replace('{year}', year)
template = template.replace('{day}', day)
template = template.replace('{file_day}', file_day)
template = template.replace('{year}', f'{year:04}')
template = template.replace('{day}', str(day))
template = template.replace('{file_day}', f'{day:02}')

return template


def read_solution_template(year: int, day: int) -> str:
template_path = os.path.join(ROOT_DIR, 'scripts/templates/day_template.txt')
return _read_solution_template(template_path, str(year), str(day))


def read_test_template(year: int, day: int) -> str:
template_path = os.path.join(ROOT_DIR, 'scripts/templates/test_template.txt')
return _read_test_template(template_path, str(year), str(day), f'{day:02}')


def write_template(filename: str, template: str):
with open(filename, 'w') as f:
f.write(template)
return _read_template(template_path, year, day)


def _parse_args(args: List[str]) -> Tuple[int, int]:
Expand Down
8 changes: 5 additions & 3 deletions src/adventofcode/scripts/get_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import requests

from adventofcode.config import ROOT_DIR
from adventofcode.util.input_helpers import get_input_directory_path,get_input_path


def get_input(year: int, day: int):
Expand All @@ -17,19 +18,20 @@ def _download_input(year: int, day: int, session: str) -> bytes:
"""
cookies = {'session': session}
url = f'https://adventofcode.com/{year}/day/{day}/input'
# TODO: put User-Agent in env variables
headers = {'User-Agent': 'github.com/AntoineGanne/adventofcode by antoine.ganne+aoc@hotmail.fr'}
resp = requests.get(url, cookies=cookies)
resp.raise_for_status()
return resp.content # type: ignore


def _save_input(data: bytes, year: int, day: int) -> None:
inputs_path = os.path.join(ROOT_DIR, 'inputs')
inputs_path = get_input_directory_path(year, day)

if not os.path.exists((year_path := os.path.join(inputs_path, str(year)))):
if not os.path.exists((year_path := os.path.join(inputs_path))):
os.mkdir(year_path)

with open(os.path.join(year_path, f'day_{day:02}.txt'), 'wb') as file:
with open(get_input_path(year,day), 'wb') as file:
file.write(data)


Expand Down
2 changes: 1 addition & 1 deletion src/adventofcode/scripts/templates/day_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from typing import List

from adventofcode.util.exceptions import SolutionNotFoundException
from adventofcode.util.helpers import solution_timer
from adventofcode.util.input_helpers import get_input_for_day
from adventofcode.util.input_helpers import get_input_for_day,get_test_input_for_day


@solution_timer({year}, {day}, 1)
Expand Down
2 changes: 1 addition & 1 deletion src/adventofcode/scripts/templates/test_template.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from adventofcode.util.input_helpers import get_test_input_for_day
from adventofcode.year_{year}.day_{file_day}_{year} import part_two, part_one
from adventofcode.year_{year}.day_{file_day}.solution_{file_day}_{year} import part_two, part_one


def test_part_one():
Expand Down
32 changes: 26 additions & 6 deletions src/adventofcode/util/input_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,42 @@ def get_input_for_day(year: int, day: int) -> List[str]:
"""
Get the input for the year/day as list of strings
"""
input_file = os.path.join(ROOT_DIR, 'inputs', str(year), f'day_{day:02}.txt')
input_file = os.path.join(get_input_directory_path(year, day), f'input_{day:02}_{year:04}.txt')
return _get_input(input_file)


def get_input_for_day_as_str(year: int, day: int) -> str: # TODO: delete this function (only used in tests)
input_file = get_input_path(year, day)
return _read_file(input_file)


def get_input_path(year: int, day: int) -> str:
return os.path.join(get_input_directory_path(year, day), f'input_{day:02}_{year:04}.txt')


def get_test_input_for_day(year: int, day: int) -> List[str]:
"""
Get the input for the year/day as list of strings
"""
input_file = os.path.join(ROOT_DIR, f'..', f'..', 'tests', f'year_{year}', f'inputs', f'day_{day:02}.txt')
print(input_file)
input_file = get_example_input_path(year, day)
return _get_input(input_file)


def get_input_for_day_as_str(year: int, day: int) -> str:
input_file = os.path.join(ROOT_DIR, 'inputs', str(year), f'day_{day:02}_input.txt')
return _read_file(input_file)
def get_example_input_path(year: int, day: int) -> str:
return os.path.join(get_input_directory_path(year, day), f'example_input_{day:02}_{year:04}.txt')


def get_test_input_module_path(year) -> str:
test_input_module_path = os.path.abspath(os.path.join(ROOT_DIR, 'test_inputs', f'{year}'))
return test_input_module_path


def get_directory_path(year, day) -> str:
return os.path.abspath(os.path.join(ROOT_DIR, f'year_{year:04}', f'day_{day:02}'))


def get_input_directory_path(year, day) -> str:
return os.path.abspath(os.path.join(get_directory_path(year, day), 'inputs'))


def _read_lines(file_name) -> List[str]:
Expand Down
31 changes: 0 additions & 31 deletions src/adventofcode/year_2019/day_01_2019.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/adventofcode/year_2021/day_01_2021.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def part_two(input_data: List[str]):
if i == 0 or i == 1:
continue
sum_depths = sum(sliding_window)
if i < 10: print(str(i) + " sum:" + str(sum_depths))
# if i < 10: print(str(i) + " sum:" + str(sum_depths))
if sum_depths > last_depth:
depth_increase_counter += 1
last_depth = sum_depths
Expand Down
Loading

0 comments on commit 6ccbce1

Please sign in to comment.