|
| 1 | +""" |
| 2 | +Project Euler Problem 116: https://projecteuler.net/problem=116 |
| 3 | +
|
| 4 | +A row of five grey square tiles is to have a number of its tiles |
| 5 | +replaced with coloured oblong tiles chosen |
| 6 | +from red (length two), green (length three), or blue (length four). |
| 7 | +
|
| 8 | +If red tiles are chosen there are exactly seven ways this can be done. |
| 9 | +
|
| 10 | + |red,red|grey|grey|grey| |grey|red,red|grey|grey| |
| 11 | +
|
| 12 | + |grey|grey|red,red|grey| |grey|grey|grey|red,red| |
| 13 | +
|
| 14 | + |red,red|red,red|grey| |red,red|grey|red,red| |
| 15 | +
|
| 16 | + |grey|red,red|red,red| |
| 17 | +
|
| 18 | +If green tiles are chosen there are three ways. |
| 19 | +
|
| 20 | + |green,green,green|grey|grey| |grey|green,green,green|grey| |
| 21 | +
|
| 22 | + |grey|grey|green,green,green| |
| 23 | +
|
| 24 | +And if blue tiles are chosen there are two ways. |
| 25 | +
|
| 26 | + |blue,blue,blue,blue|grey| |grey|blue,blue,blue,blue| |
| 27 | +
|
| 28 | +Assuming that colours cannot be mixed there are 7 + 3 + 2 = 12 ways |
| 29 | +of replacing the grey tiles in a row measuring five units in length. |
| 30 | +
|
| 31 | +How many different ways can the grey tiles in a row measuring fifty units in length |
| 32 | +be replaced if colours cannot be mixed and at least one coloured tile must be used? |
| 33 | +
|
| 34 | +NOTE: This is related to Problem 117 (https://projecteuler.net/problem=117). |
| 35 | +""" |
| 36 | + |
| 37 | + |
| 38 | +def solution(length: int = 50) -> int: |
| 39 | + """ |
| 40 | + Returns the number of different ways can the grey tiles in a row |
| 41 | + of the given length be replaced if colours cannot be mixed |
| 42 | + and at least one coloured tile must be used |
| 43 | +
|
| 44 | + >>> solution(5) |
| 45 | + 12 |
| 46 | + """ |
| 47 | + |
| 48 | + different_colour_ways_number = [[0] * 3 for _ in range(length + 1)] |
| 49 | + |
| 50 | + for row_length in range(length + 1): |
| 51 | + for tile_length in range(2, 5): |
| 52 | + for tile_start in range(row_length - tile_length + 1): |
| 53 | + different_colour_ways_number[row_length][tile_length - 2] += ( |
| 54 | + different_colour_ways_number[row_length - tile_start - tile_length][ |
| 55 | + tile_length - 2 |
| 56 | + ] |
| 57 | + + 1 |
| 58 | + ) |
| 59 | + |
| 60 | + return sum(different_colour_ways_number[length]) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + print(f"{solution() = }") |
0 commit comments