Skip to content

Commit

Permalink
day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
vinvinvincent committed Jan 6, 2024
1 parent 8202d55 commit 875bf5d
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
34 changes: 34 additions & 0 deletions aoc2023/day6.py
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)
35 changes: 35 additions & 0 deletions data/day6.py
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)
2 changes: 2 additions & 0 deletions my_venv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
. venv/bin/activate
export PYTHONPATH=`pwd`
38 changes: 38 additions & 0 deletions tests/test_day6.py
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')
2 changes: 1 addition & 1 deletion tests/test_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
COVERAGE_OPT = '--coverage'
COVERAGE = False

DAYS = [1, 2, 3, 4, 5, 8, 9, 15, 16, 19]
DAYS = [1, 2, 3, 4, 5, 6, 8, 9, 15, 16, 19]


@test_plan(name='AOC 2023', json_path='report.json')
Expand Down

0 comments on commit 875bf5d

Please sign in to comment.