Clean 2023.
This commit is contained in:
parent
d92e4744a4
commit
999207b007
@ -1,6 +1,5 @@
|
|||||||
import operator
|
import math
|
||||||
import sys
|
import sys
|
||||||
from functools import reduce
|
|
||||||
from typing import Literal, TypeAlias, cast
|
from typing import Literal, TypeAlias, cast
|
||||||
|
|
||||||
CubeType: TypeAlias = Literal["red", "blue", "green"]
|
CubeType: TypeAlias = Literal["red", "blue", "green"]
|
||||||
@ -36,9 +35,8 @@ print(f"answer 1 is {answer_1}")
|
|||||||
|
|
||||||
# part 2
|
# part 2
|
||||||
answer_2 = sum(
|
answer_2 = sum(
|
||||||
reduce(
|
math.prod(
|
||||||
operator.mul,
|
max(cube_set.get(cube, 0) for cube_set in set_of_cubes) for cube in MAX_CUBES
|
||||||
(max(cube_set.get(cube, 0) for cube_set in set_of_cubes) for cube in MAX_CUBES),
|
|
||||||
)
|
)
|
||||||
for set_of_cubes in games.values()
|
for set_of_cubes in games.values()
|
||||||
)
|
)
|
||||||
|
29
2023/day6.py
29
2023/day6.py
@ -1,22 +1,16 @@
|
|||||||
import operator
|
import math
|
||||||
import sys
|
import sys
|
||||||
from functools import reduce
|
|
||||||
|
|
||||||
lines = sys.stdin.read().splitlines()
|
|
||||||
|
|
||||||
|
|
||||||
def extreme_times_to_beat(time: int, distance: int) -> tuple[int, int]:
|
def extreme_times_to_beat(time: int, distance: int) -> tuple[int, int]:
|
||||||
import math
|
# formula (T = total time, D = target distance)
|
||||||
|
# - speed(t) = t,
|
||||||
# formula
|
# - distance(t) = (T - t) * speed(t)
|
||||||
# - distance = (T - t) * t
|
# - distance(t) > D
|
||||||
# - (T - t) * t >= D
|
# <=> (T - t) * t > D
|
||||||
# - T * t - t^2 - D >= 0
|
# <=> -t^2 + T * t - D >= 0
|
||||||
|
|
||||||
a = -1
|
|
||||||
b = time
|
|
||||||
c = -distance
|
|
||||||
|
|
||||||
|
a, b, c = -1, time, -distance
|
||||||
d = b * b - 4 * a * c
|
d = b * b - 4 * a * c
|
||||||
|
|
||||||
t1 = math.ceil(-(b - math.sqrt(d)) / (2 * a))
|
t1 = math.ceil(-(b - math.sqrt(d)) / (2 * a))
|
||||||
@ -31,18 +25,17 @@ def extreme_times_to_beat(time: int, distance: int) -> tuple[int, int]:
|
|||||||
return t1, t2
|
return t1, t2
|
||||||
|
|
||||||
|
|
||||||
|
lines = sys.stdin.read().splitlines()
|
||||||
|
|
||||||
# part 1
|
# part 1
|
||||||
times = list(map(int, lines[0].split()[1:]))
|
times = list(map(int, lines[0].split()[1:]))
|
||||||
distances = list(map(int, lines[1].split()[1:]))
|
distances = list(map(int, lines[1].split()[1:]))
|
||||||
answer_1 = reduce(
|
answer_1 = math.prod(
|
||||||
operator.mul,
|
|
||||||
(
|
|
||||||
t2 - t1 + 1
|
t2 - t1 + 1
|
||||||
for t1, t2 in (
|
for t1, t2 in (
|
||||||
extreme_times_to_beat(time, distance)
|
extreme_times_to_beat(time, distance)
|
||||||
for time, distance in zip(times, distances)
|
for time, distance in zip(times, distances)
|
||||||
)
|
)
|
||||||
),
|
|
||||||
)
|
)
|
||||||
print(f"answer 1 is {answer_1}")
|
print(f"answer 1 is {answer_1}")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user