program insane(input, output);
alfa = packed array[1..10] of char;
face = (front, back, top, bottom, left, right);
pair = (one2, three4, five6);
color = (red, blue, green, white);
index, halfindex: integer;
data: array[blockno, face] of alfa;
sum: array[blockno, pair, color] of integer;
halfsolution: array[blockno, 1..30] of pair;
function word(alf: alfa): color;
for position := front to right do
data[cube, position] := hue;
function facepair(aface: face): pair;
front, back: facepair := one2;
top, bottom: facepair := three4;
left, right: facepair := five6
for side := front to right do
for technicolor := red to white do
sum[cube, facepair(side), technicolor] := 0;
for side := front to right do
sum[cube, facepair(side), word(data[cube,side])] :=
sum[cube, facepair(side), word(data[cube,side])] + 1;
subtotals: array[red..white] of integer;
pair1, pair2, pair3, pair4: pair;
function two222(pair1, pair2, pair3, pair4: pair): Boolean;
for hue := red to white do
if (subtotals[red]=2) and
(subtotals[white]=2) then
halfsolution[1, halfindex] := pair1;
halfsolution[2, halfindex] := pair2;
halfsolution[3, halfindex] := pair3;
halfsolution[4, halfindex] := pair4;
halfindex := halfindex + 1;
for pair1 := one2 to five6 do
for pair2 := one2 to five6 do
for pair3 := one2 to five6 do
for pair4 := one2 to five6 do
if two222(pair1, pair2, pair3, pair4) then
if (halfsolution[1, pointr]<>halfsolution[1,index]) and
(halfsolution[2, pointr]<>halfsolution[2,index]) and
(halfsolution[3, pointr]<>halfsolution[3,index]) and
(halfsolution[4, pointr]<>halfsolution[4,index]) then
until done or (index = pred(halfindex));
until done or (pointr = halfindex);
if pointr = halfindex then
procedure put(a, b: pair);
old1, new1, old2, new2: face;
procedure oldpair(c: pair);
procedure newpair(d: pair);
save1 := data[box, new1];
data[box, new1] := data[box, old1];
data[box, old1] := save1;
save2 := data[box, new2];
data[box, new2] := data[box, old2];
data[box, old2] := save2;
a := halfsolution[box, pointr];
b := halfsolution[box, index];
if (a=one2) and (b=five6) then
list: array[1..8] of integer;
delux: array[red..white] of integer;
for kolor := red to white do
delux[word(data[counter,side])] :=
delux[word(data[counter,side])] + 1;
if delux[word(data[counter,side])] >= 2 then
save := data[list[counter], side];
data[list[counter], side] := data[list[counter], opposite];
data[list[counter], opposite] := save;
for side := back to top do
writeln('no solutions') else
writeln('solution to instant insanity');
for side := front to bottom do