Skip to content

Instantly share code, notes, and snippets.

@taroyabuki
Last active December 25, 2019 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taroyabuki/a906b537b1230e3bb29fba4f8f71845c to your computer and use it in GitHub Desktop.
Save taroyabuki/a906b537b1230e3bb29fba4f8f71845c to your computer and use it in GitHub Desktop.
map = Import["http://mathforum.org/wagon/fall97/images/5colormap.gif"];
matrix = MorphologicalComponents[map];
vars = Flatten[Map[{p[#], q[#]} &, Rest[Union[Flatten[matrix]]]]];
f[r_] := Map[If[Length[Union[#]] > 2, Sort[Select[#, Positive]], Nothing] &,
Partition[r, 4, 1]];
neighbours = Union[Flatten[Map[f, Join[matrix, Transpose[matrix]]], 1]];
eqns = Apply[And,
Map[BooleanConvert[Or[
Xor[p[#[[1]]], p[#[[2]]]],
Xor[q[#[[1]]], q[#[[2]]]]], "CNF"] &, neighbours]];
sol = FindInstance[eqns, vars, Booleans];
colors = {
{True, True} -> RGBColor["#ea5415"],
{True, False} -> RGBColor["#1d7a21"],
{False, True} -> RGBColor["#005192"],
{False, False} -> RGBColor["#ffdf00"]};
cTable = Table[i -> {p[i], q[i]} /. sol[[1]] /. colors, {i, 2, Length[vars]/2}];
ArrayPlot[matrix,
Frame -> False,
ColorRules -> Append[cTable, 0 -> Black]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment