Skip to content

Commit c918eed

Browse files
committed
Structuring as a package and add tests based on puzzle examples
1 parent 9bd557d commit c918eed

File tree

13 files changed

+132
-10
lines changed

13 files changed

+132
-10
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
*.pyc
2+
.pytest_cache
3+
.tox
4+
*egg-info
5+
venv

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: python
2+
python:
3+
- "2.7"
4+
- "3.6"
5+
install:
6+
- pip install tox-travis
7+
script:
8+
- tox

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Build Status](https://travis-ci.org/Nani-o/adventofcode2018.svg?branch=master)](https://travis-ci.org/Nani-o/adventofcode2018)
2+
13
Advent of code 2018
24
===================
35

aoc/__init__.py

Whitespace-only changes.

adventofcode.py renamed to aoc/aoc.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from argparse import ArgumentParser
66

77
script_path = os.path.dirname(os.path.realpath(__file__))
8-
input_path = os.path.join(script_path, 'inputs')
8+
input_path = os.path.join(script_path, '../inputs')
99

1010
def get_input(day):
1111
input_file = os.path.join(input_path, "day{}".format(day))
@@ -15,11 +15,11 @@ def get_input(day):
1515
return puzzle_input
1616

1717
def get_solver(day, part):
18-
puzzle_file = "day{}".format(day)
19-
puzzle_part_func = "part{}".format(part)
18+
submodule_name = "day{}".format(day)
19+
function_name = "part{}".format(part)
2020

21-
puzzle = __import__(name=puzzle_file)
22-
solve_func = getattr(puzzle, puzzle_part_func)
21+
module = __import__(name="aoc.{}".format(submodule_name))
22+
solve_func = getattr(getattr(module, submodule_name), function_name)
2323
return solve_func
2424

2525
def timeit(func, arg):
@@ -28,7 +28,7 @@ def timeit(func, arg):
2828
end = time.time()
2929
elapsed = (end - start) * 1000
3030
return result, elapsed
31-
31+
3232
def get_message(day, part, solution, elapsed):
3333
title = "Day {} - part {}".format(day, part)
3434
title = "{}\n".format(title) + "=" * len(title)
@@ -38,7 +38,7 @@ def get_message(day, part, solution, elapsed):
3838
message = "{}\n\n{}\n{}\n".format(title, result, time)
3939
return message
4040

41-
if __name__ == '__main__':
41+
def main():
4242
parser = ArgumentParser()
4343
parser.add_argument('--day', '-d', type=int, help='Day of the puzzle to run')
4444
parser.add_argument('--part', '-p', type=int, help='Part of the puzzle of the day to run')
@@ -47,6 +47,9 @@ def get_message(day, part, solution, elapsed):
4747
solve_func = get_solver(args.day, args.part)
4848
puzzle_input = get_input(args.day)
4949
solution, elapsed = timeit(solve_func, puzzle_input)
50-
50+
5151
message = get_message(args.day, args.part, solution, elapsed)
5252
print(message)
53+
54+
if __name__ == '__main__':
55+
main()

day1.py renamed to aoc/day1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/env python
22

33
def part1(puzzle_input):
4-
frequency_changes = map(int, puzzle_input.split())
4+
frequency_changes = list(map(int, puzzle_input.split()))
55
return sum(frequency_changes)
66

77
def part2(puzzle_input):
8-
frequency_changes = map(int, puzzle_input.split())
8+
frequency_changes = list(map(int, puzzle_input.split()))
99

1010
current_frequency_result = 0
1111
frequency_results = set([current_frequency_result])

day2.py renamed to aoc/day2.py

File renamed without changes.

day3.py renamed to aoc/day3.py

File renamed without changes.

setup.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
import os
3+
from setuptools import setup
4+
5+
setup(
6+
name = "aoc",
7+
version = "0.0.1",
8+
author = "Sofiane Medjkoune",
9+
description = ("A simple cli to run advent of code 2018 puzzles"),
10+
license = "MIT",
11+
keywords = "adventofcode",
12+
packages=['aoc'],
13+
entry_points = {
14+
'console_scripts': ['aoc=aoc.aoc:main'],
15+
},
16+
)

tests/test_day1.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
3+
import pytest
4+
from aoc.day1 import part1, part2
5+
6+
test_inputs = [
7+
"+1\n-2\n+3\n+1",
8+
"+1\n-1",
9+
"+3\n+3\n+4\n-2\n-4",
10+
"-6\n+3\n+8\n+5\n-6",
11+
"+7\n+7\n-2\n-7\n-4"
12+
]
13+
14+
expected_part1 = [3, 0, 4, 4, 1]
15+
expected_part2 = [2, 0, 10, 5, 14]
16+
17+
@pytest.mark.parametrize("input,expected", list(zip(test_inputs, expected_part1)))
18+
def test_part1(input, expected):
19+
solution = part1(input)
20+
assert solution == expected
21+
22+
@pytest.mark.parametrize("input,expected", list(zip(test_inputs, expected_part2)))
23+
def test_part2(input, expected):
24+
solution = part2(input)
25+
assert solution == expected

0 commit comments

Comments
 (0)