A retro game console's video chip cannot display arbitrary 24-bit colors: each of its red, green, and blue channels can only be set to one of sixteen brightness levels, and internally every channel is stored as a single hexadecimal digit repeated twice -- so any color the console can actually render has a six-digit hex code whose digits come in three equal pairs. An art tool needs to convert an artist's freely-chosen source color into the closest color the console can render.
Given a source color as a 6-digit hexadecimal RGB string, output the console-representable color (also a 6-digit hexadecimal RGB string, with each channel's two digits equal) that minimizes the sum, over the three channels, of the squared difference between the source channel value and the chosen channel value. Each channel can be optimized independently of the other two. No channel value in the test data is ever exactly equidistant between two supported levels, so the closest level for each channel is always unique.
A single line containing a string of the form "#RRGGBB": a '#' character followed by exactly 6 lowercase hexadecimal digits (0-9, a-f).
A single line containing the nearest console-representable color in the same "#RRGGBB" format, using lowercase hexadecimal digits, where digits 1-2 are equal, digits 3-4 are equal, and digits 5-6 are equal.
Example 1
Input
#09f166
Expected
#11ee66
Explanation
Red 0x09=9 is closer to level 0x11=17 (distance 8) than to 0x00 (distance 9), giving digit '1'. Green 0xf1=241 is closer to level 0xee=238 (distance 3) than to 0xff (distance 14), giving digit 'e'. Blue 0x66=102 exactly matches the supported level 0x66 (distance 0), giving digit '6'. The result is "#11ee66".
Example 2
Input
#4e3fe1
Expected
#5544dd
Explanation
Red 0x4e=78 is closer to level 0x55=85 (distance 7) than to 0x44=68 (distance 10), giving digit '5'. Green 0x3f=63 is closer to level 0x44=68 (distance 5) than to 0x33=51 (distance 12), giving digit '4'. Blue 0xe1=225 is closer to level 0xdd=221 (distance 4) than to 0xee=238 (distance 13), giving digit 'd'. The result is "#5544dd".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →