|
| 1 | +""" |
| 2 | +Project Euler Problem 115: https://projecteuler.net/problem=115 |
| 3 | +
|
| 4 | +NOTE: This is a more difficult version of Problem 114 |
| 5 | +(https://projecteuler.net/problem=114). |
| 6 | +
|
| 7 | +A row measuring n units in length has red blocks |
| 8 | +with a minimum length of m units placed on it, such that any two red blocks |
| 9 | +(which are allowed to be different lengths) are separated by at least one black square. |
| 10 | +
|
| 11 | +Let the fill-count function, F(m, n), |
| 12 | +represent the number of ways that a row can be filled. |
| 13 | +
|
| 14 | +For example, F(3, 29) = 673135 and F(3, 30) = 1089155. |
| 15 | +
|
| 16 | +That is, for m = 3, it can be seen that n = 30 is the smallest value |
| 17 | +for which the fill-count function first exceeds one million. |
| 18 | +
|
| 19 | +In the same way, for m = 10, it can be verified that |
| 20 | +F(10, 56) = 880711 and F(10, 57) = 1148904, so n = 57 is the least value |
| 21 | +for which the fill-count function first exceeds one million. |
| 22 | +
|
| 23 | +For m = 50, find the least value of n |
| 24 | +for which the fill-count function first exceeds one million. |
| 25 | +""" |
| 26 | + |
| 27 | +from itertools import count |
| 28 | + |
| 29 | + |
| 30 | +def solution(min_block_length: int = 50) -> int: |
| 31 | + """ |
| 32 | + Returns for given minimum block length the least value of n |
| 33 | + for which the fill-count function first exceeds one million |
| 34 | +
|
| 35 | + >>> solution(3) |
| 36 | + 30 |
| 37 | +
|
| 38 | + >>> solution(10) |
| 39 | + 57 |
| 40 | + """ |
| 41 | + |
| 42 | + fill_count_functions = [1] * min_block_length |
| 43 | + |
| 44 | + for n in count(min_block_length): |
| 45 | + fill_count_functions.append(1) |
| 46 | + |
| 47 | + for block_length in range(min_block_length, n + 1): |
| 48 | + for block_start in range(n - block_length): |
| 49 | + fill_count_functions[n] += fill_count_functions[ |
| 50 | + n - block_start - block_length - 1 |
| 51 | + ] |
| 52 | + |
| 53 | + fill_count_functions[n] += 1 |
| 54 | + |
| 55 | + if fill_count_functions[n] > 1_000_000: |
| 56 | + break |
| 57 | + |
| 58 | + return n |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + print(f"{solution() = }") |
0 commit comments