-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8202d55
commit 875bf5d
Showing
5 changed files
with
110 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from data.day6 import Race | ||
|
||
|
||
def get_ways_to_win(race: Race): | ||
ways = 0 | ||
for hold_sec in range(1, race.time + 1): | ||
remaining_sec = race.time - hold_sec | ||
# print( | ||
# f'hold_sec={hold_sec}, remaining_sec={remaining_sec}, dist={remaining_sec * hold_sec}, race.distance={race.distance}' | ||
# ) | ||
if remaining_sec > 0: | ||
if remaining_sec * hold_sec > race.distance: | ||
ways += 1 | ||
else: | ||
if ways == 0: | ||
continue | ||
else: | ||
return ways | ||
return ways | ||
|
||
|
||
def get_product_of_ways(races: list): | ||
ways = 1 | ||
for i in range(len(races)): | ||
ways *= get_ways_to_win(races[i]) | ||
return ways | ||
|
||
|
||
def get_part1(args): | ||
return get_product_of_ways(args) | ||
|
||
|
||
def get_part2(args): | ||
return get_ways_to_win(args) |
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,35 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Race: | ||
time: int | ||
distance: int | ||
|
||
|
||
def parse_race_data(text): | ||
lines = text.strip().split('\n') | ||
time_values = [int(value) for value in lines[0].split()[1:]] | ||
distance_values = [int(value) for value in lines[1].split()[1:]] | ||
race_list = [ | ||
Race(time, distance) for time, distance in zip(time_values, distance_values) | ||
] | ||
return race_list | ||
|
||
|
||
SAMPLE_DATA_TEXT = ''' | ||
Time: 7 15 30 | ||
Distance: 9 40 200''' | ||
|
||
PUZZLE_DATA_TEXT = '''Time: 53 83 72 88 | ||
Distance: 333 1635 1289 1532''' | ||
|
||
|
||
SAMPLE_DATA = parse_race_data(SAMPLE_DATA_TEXT) | ||
PUZZLE_DATA = parse_race_data(PUZZLE_DATA_TEXT) | ||
|
||
SAMPLE_DATA_PART1 = SAMPLE_DATA | ||
PUZZLE_DATA_PART1 = PUZZLE_DATA | ||
|
||
SAMPLE_DATA_PART2 = Race(71530, 940200) | ||
PUZZLE_DATA_PART2 = Race(53837288, 333163512891532) |
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,2 @@ | ||
. venv/bin/activate | ||
export PYTHONPATH=`pwd` |
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,38 @@ | ||
import pytest | ||
from testplan.testing.multitest.result import Result | ||
from timeout_function_decorator import timeout | ||
from utils import TIMEOUT_SEC, check_eq_or_log | ||
|
||
from data.day6 import ( | ||
SAMPLE_DATA_PART1, | ||
PUZZLE_DATA_PART1, | ||
SAMPLE_DATA_PART2, | ||
PUZZLE_DATA_PART2, | ||
) | ||
from aoc2023.day6 import get_part1, get_part2 | ||
|
||
|
||
class TestDay6: | ||
@timeout(TIMEOUT_SEC) | ||
@pytest.mark.parametrize( | ||
'input, expected', | ||
[ | ||
(SAMPLE_DATA_PART1, 288), | ||
(PUZZLE_DATA_PART1, None), | ||
], | ||
) | ||
def test_part1(self, env, result: Result, input, expected): | ||
'''Part 1''' | ||
check_eq_or_log(result, get_part1(input), expected, 'Part 1') | ||
|
||
@timeout(90) | ||
@pytest.mark.parametrize( | ||
'input, expected', | ||
[ | ||
(SAMPLE_DATA_PART2, 71503), | ||
(PUZZLE_DATA_PART2, None), | ||
], | ||
) | ||
def test_part2(self, env, result: Result, input, expected): | ||
'''Part 2''' | ||
check_eq_or_log(result, get_part2(input), expected, 'Part 2') |
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