-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
3 changed files
with
55 additions
and
0 deletions.
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,3 @@ | ||
__year__ = 2023 | ||
__day__ = 6 | ||
__title__ = "Wait For It" |
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 @@ | ||
Time: 7 15 30 | ||
Distance: 9 40 200 |
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,50 @@ | ||
import math | ||
|
||
from pydantic.dataclasses import dataclass | ||
|
||
from aoc.lib.solution import SolutionBase | ||
|
||
import logging | ||
|
||
|
||
__all__ = ["Solution"] | ||
__log__ = logging.getLogger(__name__) | ||
|
||
|
||
@dataclass | ||
class Data: | ||
time: tuple[int, ...] | ||
distance: tuple[int, ...] | ||
|
||
|
||
class Solution(SolutionBase): | ||
@staticmethod | ||
def prepare(data): | ||
return Data( | ||
time=tuple(map(int, data[0][5:].split())), | ||
distance=tuple(map(int, data[1][9:].split())), | ||
) | ||
|
||
@staticmethod | ||
def ways(time, distance): | ||
sqrt = math.sqrt(time**2 - 4 * distance) | ||
return math.ceil(0.5 * (time + sqrt)) - math.floor(0.5 * (time - sqrt)) - 1 | ||
|
||
@staticmethod | ||
def part_01(data): | ||
return math.prod( | ||
Solution.ways(time, distance) | ||
for time, distance in zip(data.time, data.distance) | ||
) | ||
|
||
@staticmethod | ||
def part_02(data): | ||
time = int("".join(map(str, data.time))) | ||
distance = int("".join(map(str, data.distance))) | ||
return Solution.ways(time, distance) | ||
|
||
|
||
if __name__ == "__main__": | ||
import aoc.lib.main | ||
|
||
aoc.lib.main.main(Solution) |