Георги обнови решението на 18.10.2022 23:27 (преди около 2 години)
+def calculate_final_vector(init_vector, hexes):
+ """Calculate final vector based on initial vector and list of hexes."""
+ # Unpack the input tuple so that its coordinates are mutable objects
+ x, y = init_vector
+ # Define light/dark saturation with their corresponding steps and
+ # full/empty values for color definition
+ saturations = (
+ {
+ 'units': {
+ 'full': 'ff',
+ 'empty': 'c0'
+ },
+ 'step': -1,
+ },
+ {
+ 'units': {
+ 'full': 'c0',
+ 'empty': '00'
+ },
+ 'step': +1,
+ }
+ )
+ # Map the hex list to lower strings and process each hex one by one
+ for hex in map(str.lower, hexes):
+ # Determine hex type
+ if hex == 'ffffff':
+ # White colors means no instruction - skip the current hex
+ continue
+ elif hex == '000000':
+ # Black colors means to halt - stop the loop
+ break
+ else:
+ # Split color into RGB components to ensure it fits into
+ # one of the expected saturation definitions
+ r, g, b = hex[:2], hex[2:4], hex[4:]
+ # Try to find matching saturation
+ for satur in saturations:
+ if all(x in satur['units'].values() for x in (r, g, b)):
+ # Take leaf-level values from the saturation
+ # definition for better code readbility
+ full = satur['units']['full']
+ empty = satur['units']['empty']
+ # Determine the color and perform the action
+ if (r, g, b) == (full, empty, empty): # Red
+ x -= satur['step']
+ elif (r, g, b) == (full, full, empty): # Yellow
+ y += satur['step']
+ elif (r, g, b) == (empty, full, empty): # Green
+ x += satur['step']
+ elif (r, g, b) == (empty, empty, full): # Blue
+ y -= satur['step']
+ else:
+ raise ValueError(f'Invalid color hex: {hex}')
+ break
+ # If there was no matching saturation, meaning no break,
+ # that indicates invalid input
+ else:
+ raise ValueError(f'Invalid saturation for: {hex}.')
+ return x, y
-