Go: package main

import (
	"fmt"
	"math"
	"strconv"
)

// ColorCode is a string like #rrggbb
type ColorCode string

// ColorName is a name of color
type ColorName string

// Color has colorname and colorcode
type Color struct {
	name ColorName
	code ColorCode
}

// ColorTable has default colors
type ColorTable struct {
	colors []Color
}

var (
	colorTable = ColorTable{
		[]Color{
			Color{"black", "#000000"},
			Color{"blue", "#0000ff"},
			Color{"lime", "#00ff00"},
			Color{"aqua", "#00ffff"},
			Color{"red", "#ff0000"},
			Color{"fuchsia", "#ff00ff"},
			Color{"yellow", "#ffff00"},
			Color{"white", "#ffffff"},
		},
	}
)

func main() {
	var code ColorCode
	fmt.Scanf("%s", &code)
	for {
		if code == "0" {
			break
		}
		name := colorTable.getNearestColor(code)
		fmt.Println(name)
		fmt.Scanf("%s", &code)
	}
}

func (colorstr ColorCode) parseColorValue() (float64, float64, float64) {
	rr, gg, bb := colorstr[1:3], colorstr[3:5], colorstr[5:7]
	rri, err1 := strconv.ParseUint(string(rr), 16, 8)
	ggi, err2 := strconv.ParseUint(string(gg), 16, 8)
	bbi, err3 := strconv.ParseUint(string(bb), 16, 8)
	if err1 != nil || err2 != nil || err3 != nil {
		err := fmt.Errorf(
			"Failed to read color strings, color code:%s, err: %v %v %v",
			colorstr, err1, err2, err3)
		panic(err)
	}
	return float64(rri), float64(ggi), float64(bbi)
}

func (table *ColorTable) getNearestColor(targetcode ColorCode) ColorName {
	var nearestColor ColorName
	var nearestValue = 1e5
	for _, v := range table.colors {
		trr, tgg, tbb := targetcode.parseColorValue()
		prr, pgg, pbb := v.code.parseColorValue()
		// diff := math.Sqrt(
		// 	math.Pow(prr-trr, 2) + math.Pow(pgg-tgg, 2) + math.Pow(pbb-tbb, 2))
		diff := math.Pow(prr-trr, 2) +
			math.Pow(pgg-tgg, 2) +
			math.Pow(pbb-tbb, 2)
		if nearestValue > diff {
			nearestValue = diff
			nearestColor = v.name
		}
	}
	return nearestColor
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
//#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

string str[8]={"black",
                "blue",
                "lime",
                "aqua",
                "red",
                "fuchsia",
                "yellow",
                "white"};

int change(string t){
    int res = 0;
    rep(i, 0, 2){
        if('0' <= t[i] && t[i] <= '9'){
            res += (t[i] - '0')*16*(1 - i);
        }else{
            res += ((t[i] - 'a') + 10)*16*(1 - i);
        }
    }
    return res;
}

signed main() {
    string s;
    while(cin >> s){
        if(s == "0") break;
        int r, g, b;
        r = change(s.substr(1, 2));
        g = change(s.substr(3, 2));
        b = change(s.substr(5, 2));
        int MIN = 1e9, p = -1;
        rep(i, 0, 2){
            rep(j, 0, 2){
                rep(k, 0, 2){
                    int d = (r - i*16*16)*(r - i*16*16) + (g - j*16*16)*(g - j*16*16) + (b - k*16*16)*(b - k*16*16);
                    //o(d);
                    if(d < MIN){
                        MIN = d;
                        p = i*4 + j*2 + k;
                    }
                }
            }
        }
        cout << str[p] << endl;
    }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

/*
	ツつ「ツづつつゥツカツδ可ーツコツーツドツδ可イツブツδ可δ環づつつュツづォツつ、
 */
struct Color
{
	int r, g, b;
	
	Color() : r(0), g(0), b(0) { }
	Color(int r, int g, int b) : r(r), g(g), b(b)
	{	}
	Color(string str)
	{
		r = strtol(str.substr(1, 2).c_str(), NULL, 16);
		g = strtol(str.substr(3, 2).c_str(), NULL, 16);
		b = strtol(str.substr(5, 2).c_str(), NULL, 16);
	}
};

Color operator - (const Color& left, const Color& right)
{	return Color(left.r - right.r, left.g - right.g, left.b - right.b);	}

template<class T> T SQR(T x){ return x * x; }

double abs(Color color)
{	return sqrt(SQR(color.r) + SQR(color.g) + SQR(color.b));	}

int main()
{
    string name[] = { "black", "blue", "lime", "aqua", "red", "fuchsia", "yellow", "white" };
	Color colors[8];
	
	rep(i, 8) colors[i] = Color(bool(i & 1 << 2) * 0xff, bool(i & 1 << 1) * 0xff, bool(i & 1) * 0xff);
	
	for(string line; cin >> line && line != "0";)
	{
		int index = 0;
		double fit = 1 << 30;
		
		rep(i, 8) if(abs(colors[i] - Color(line)) < fit)
		{
			index = i;
			fit = abs(colors[i] - Color(line));
		}
		
		cout << name[index] << endl;
	}
}

C++: #include <iostream>
#include <string>
using namespace std;
int m[8][3] = {
  {0,0,0}, {0,0,0xff}, {0,0xff,0}, {0,0xff,0xff},
  {0xff,0,0}, {0xff,0,0xff}, {0xff,0xff,0}, {0xff,0xff,0xff},
};
string s[8] = {
  "black", "blue", "lime", "aqua", "red", "fuchsia", "yellow", "white",
};
int conv(string s)
{
  int n=0,i;
  for(i=0;i<2;i++){
    n *= 16;
    if('0' <= s[i] && s[i] <= '9') n += s[i] - '0';
    else n += s[i] - 'a' + 10;
  }
  return n;
}
int main()
{
  string i;
  for(;;){
    cin >> i;
    if(i=="0") break;
    int r,g,b;
    r = conv(i.substr(1,2));
    g = conv(i.substr(3,2));
    b = conv(i.substr(5,2));
    int d,e=0x1fffffff,j,k;
    for(j=0;j<8;j++){
      d = (r-m[j][0])*(r-m[j][0])+(g-m[j][1])*(g-m[j][1])+(b-m[j][2])*(b-m[j][2]);
      if(e>d) {
	e = d;
	k = j;
      }
    }
    cout << s[k] << endl;
  }
  return 0;
}

C++: #include <iostream>
using namespace std;

int dir[5010];

void remove(){
	int cnt = 0;
	for(int i = 0 ; i < 5010 ; i++){
		if( dir[i] == 31 ) dir[i] = 0,cnt++;
	}
	for(int k = 0 ; k <= cnt ; k++)
	for(int i = 5009 ; i > 0 ; i--){
		if( dir[i] && !dir[i-1] ){
			swap(dir[i],dir[i-1]);
		}
	}
}

void tate(int x,int l){
	for(int i = 1 ; i <= l ; i++) dir[5010-i] |= (1<<x);
	
	for(int _ = 0 ; _ < 10 ; _++)
	for(int i = 5010-l+_ ; i > 0 ; i--){
		if( (dir[i] >> x & 1) && (dir[i-1] >> x & 1) == 0 ){
			dir[i] -= (1<<x);
			dir[i-1] += (1<<x);
		}else{
			break;
		}
	}
	remove();
}

void yoko(int x,int l){
	int mask = ((1<<l)-1) << x;
	dir[5009] = mask;
	for(int i = 5009 ; i > 0 ; i--){
		if( dir[i] && !(dir[i-1] & mask) ){
			dir[i] -= mask;
			dir[i-1] += mask;
		}else{
			break;
		}
	}
	remove();
}
int main(){
	int n;
	while(cin >> n && n){
		for(int i = 0 ; i <= 5009 ; i++)
			dir[i] = 0;
		
		for(int i = 0 ; i < n ; i++){
			int d,l,x;
			cin >> d >> l >> x;
			x--;
			if(d==1){
				yoko(x,l);
			}else{
				tate(x,l);
			}
			int f = 0;
			if( i == 10 ) continue;
			for(	int j = 5009 ; j >= 0 ; j--){
				if(f||dir[j]){
					//cout << j << " " << (dir[j]&1) << (dir[j]&2) << (dir[j]&4) << (dir[j]&8) << (dir[j]&16) << endl; ;
					f=true;
				}
			}
		//	cout << endl;
		}
		int ans = 0;
		for(int i = 0 ; i < 5010 ; i++)
			ans += __builtin_popcount(dir[i]);
		cout << ans << endl;
	}
}