The following text file contains a message that has been encoded using the K3-code or the three-color code.

~~n>868/Su79{3532w5=16dD$pK|67P44Q=,416Q]+c856yi8+pX96`72B6.
tj~1_60Mr%PL}04Q45>[g=523*KW3534414/F047q_jA@l52v3s8goo5.\05
BWd<427D2\08U15x{}419F'?M835Lo2?mI487i=zD=y00G?2092Bw,<!&D`^
1hF!!98!p

The K3-code uses three types of characters that are indicated by three different colors: punctuation marks (red), letters (green) and digits (blue). No distinction is made between uppercase and lowercase letters.

~~n>868/Su79{3532w5=16dD$pK|67P44Q=,416Q]+c856yi8+pX96`72B6.
tj~1_60Mr%PL}04Q45>[g=523*KW3534414/F047q_jA@l52v3s8goo5.\05
BWd<427D2\08U15x{}419F'?M835Lo2?mI487i=zD=y00G?2092Bw,<!&D`^
1hF!!98!p

The number of characters on each line of the text file is a multiple of three, so that the characters/colors can be grouped into groups of three characters.

▪▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪ ▪▪▪ ▪ ▪▪▪ ▪▪ ▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪ ▪▪ ▪▪▪ ▪ ▪▪▪ ▪▪ 
▪▪▪ ▪ ▪▪▪ ▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪▪ ▪▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪ ▪ ▪▪ ▪▪ ▪▪▪
▪▪▪ ▪▪ ▪▪ ▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪ ▪▪ ▪▪▪ ▪ ▪▪▪ ▪▪ ▪▪▪ ▪▪ ▪ ▪▪▪ ▪▪▪ ▪▪▪ ▪▪
▪▪ ▪▪▪ ▪

With three different colors (represented by the letters R, G and B) a total of 27 combinations can be formed. The fact that each combination of three consecutive color codes encodes for one character of the original message, is the key to the K3-code.

BBB () ⟶ O                GBB () ⟶                  RBB () ⟶ A
BBG () ⟶ F                GBG () ⟶ H                RBG () ⟶ G
BBR () ⟶ P                GBR () ⟶ M                RBR () ⟶ Z
BGB () ⟶ Y                GGB () ⟶ Q                RGB () ⟶ C
BGG () ⟶ I                GGG () ⟶ L                RGG () ⟶ K
BGR () ⟶ D                GGR () ⟶ E                RGR () ⟶ B
BRB () ⟶ R                GRB () ⟶ W                RRB () ⟶ J
BRG () ⟶ N                GRG () ⟶ U                RRG () ⟶ V
BRR () ⟶ X                GRR () ⟶ T                RRR () ⟶ S

If we apply this key to the encoded message, we get

▪▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪ ▪▪▪ ▪ ▪▪▪ ▪▪ ▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪ ▪▪ ▪▪▪ ▪ ▪▪▪ ▪▪ 
 V   A   N       A   F   R   I   K   A       T   O   T       I   N       A   M

▪▪▪ ▪ ▪▪▪ ▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪▪ ▪▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪ ▪ ▪▪ ▪▪ ▪▪▪
 E   R   I   K   A       V   A   N       O   P       D   E       H   I   M   A

▪▪▪ ▪▪ ▪▪ ▪▪ ▪▪ ▪▪ ▪▪▪ ▪▪ ▪▪ ▪▪▪ ▪ ▪▪▪ ▪▪ ▪▪▪ ▪▪ ▪ ▪▪▪ ▪▪▪ ▪▪▪ ▪▪
 L   A   Y   A       T   O   T       I   N       D   E       W   O   E   S   T

▪▪ ▪▪▪ ▪
 I   J   N

from which we can read the original message as

VAN AFRIKA TOT IN AMERIKA VAN OP DE HIMALAYA TOT IN DE WOESTIJN

Assignment

The key of a K3-code is stored in a text file that contains 27 lines. Each line consists of a character, followed by a comma (,) and a unique combination of three colors (represented by the letters R, G and B) that encode the character. Note that the character may also be a comma.

U,GRG
D,BGR
 ,GBB
O,BBB
N,BRG
…
A,RBB
E,GGR
M,GBR

Your task:

Example

In the following interactive session we assume the text files key.txt1 and message.txt2 to be located in the current directory.

>>> key = read_key('key.txt3')
>>> key['RGB']
'C'
>>> key['GBB']
' '
>>> key['RRG']
'V'

>>> color('?')
'R'
>>> color('w')
'G'
>>> color('8')
'B'

>>> decode('message.txt4', key)
'VAN AFRIKA TOT IN AMERIKA VAN OP DE HIMALAYA TOT IN DE WOESTIJN'

Epilogue

The encoded messages used while testing your submitted solution are all extracted from K35 lyrics: an all-female Belgian-Dutch music group with a Dutch repertoire primarily enjoyed by pre-adolescent children. The group's name is derived from first letters of the three original members' first names: Karen Damen6, Kristel Verbeke7 and Kathleen Aerts8.

Bacons alfabet
Performance of K3 with Karen Damen, Kathleen Aerts and Kristel Verbeke in Hasselt, Belgium (2006).

Resources