Skip to content

Commit

Permalink
Solve day 6 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkram committed Dec 16, 2023
1 parent 0f2c942 commit 6dc4b8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
19 changes: 13 additions & 6 deletions 2023/day06/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
INPUTS_FILE = Path(__file__).parent / "input.txt"


def parse(input_str: str) -> None:
def parse(input_str: str, concat: bool) -> None:
times = []
distances = []
for line in input_str.splitlines():
Expand All @@ -18,7 +18,15 @@ def parse(input_str: str) -> None:
elif line.startswith("Distance:"):
distances.extend(int(d) for d in line.split()[1:])

races = list(zip(times, distances))
if not concat:
races = list(zip(times, distances))
else:
races = [
(
int("".join(str(t) for t in times)),
int("".join(str(d) for d in distances)),
)
]
return races


Expand All @@ -34,9 +42,8 @@ def count_ways_to_win(time, distance):
return time - (t * 2) + 1


def calculate(input_str: str) -> int:
print()
races = parse(input_str)
def calculate(input_str: str, concat: bool = False) -> int:
races = parse(input_str, concat=concat)

ways_to_win = []
for time, distance in races:
Expand All @@ -50,7 +57,7 @@ def main() -> None:
with INPUTS_FILE.open() as fp:
input_str = fp.read()
print(f"The answer to part 1 is {calculate(input_str)}")
print(f"The answer to part 2 is {calculate(input_str)}")
print(f"The answer to part 2 is {calculate(input_str, concat=True)}")


if __name__ == "__main__":
Expand Down
37 changes: 9 additions & 28 deletions 2023/day06/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,12 @@ def test_part1(input_str: str, expected: int) -> None:
assert calculate(input_str) == expected


# @pytest.mark.parametrize(
# "input_str,expected",
# [
# pytest.param(TEST_INPUT, 46, id="test-input"),
# pytest.param(REAL_INPUT, 24261545, id="real-data"),
# ],
# )
# def test_part2(input_str: str, expected: int) -> None:
# assert calculate(input_str, ranges=True) == expected
#
#
# @pytest.mark.parametrize(
# "rng, expected_result",
# [
# pytest.param((0, 49), {(0, 49)}, id="no-overlap"),
# pytest.param((55, 74), {(57, 76)}, id="map-overlaps_range"),
# pytest.param((55, 85), {(57, 77), (76, 85)}, id="partial-overlap-left"),
# pytest.param((40, 70), {(40, 49), (52, 72)}, id="partial-overlap-right"),
# pytest.param((40, 85), {(40, 49), (52, 77), (76, 85)}, id="range-overlaps-map"),
# ],
# )
# def test_apply_maps_to_range(rng, expected_result):
# maps = [
# (50, 98, 2), # {98 - 99}, -48
# (52, 50, 26), # {50 - 75}, +2
# ]
# result = apply_maps_to_range(rng, maps)
# assert result == expected_result
@pytest.mark.parametrize(
"input_str,expected",
[
pytest.param(TEST_INPUT, 71503, id="test-input"),
pytest.param(REAL_INPUT, 27102791, id="real-data"),
],
)
def test_part2(input_str: str, expected: int) -> None:
assert calculate(input_str, concat=True) == expected

0 comments on commit 6dc4b8c

Please sign in to comment.