/* Coloring countries in Picat. Translated from the Prolog code in http://www.inf.ed.ac.uk/teaching/courses/lp/slides/lp5.pdf slide 7ff. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go ?=> colour_countries(Colours), println(Colours.sort()), nl. % % 1) Check that the solution is correct % 2) Generate all solutions. There are 7776 different solutions. % go2 ?=> Map = get_global_map(), Map.put(count,0), colour_countries(Colours), println(Colours.sort()), % and then we check that it's correct M = new_map([Country=Colour : $c(Country,Colour) in Colours]), foreach(Country in M.keys().sort()) Colour = M.get(Country), println(Country=Colour), Neighbours = findall(C, ngb(Country,C)).flatten().sort(), println("\tNeighbours:"), foreach(N in Neighbours) Colour2 = M.get(N), printf("\t%w = %w\n",N,Colour2), if Colour2 == Colour then println("ERROR!") end end end, nl, Map.put(count,Map.get(count)+1), fail, nl. go2 => println(count=get_global_map().get(count)). neighbour(Country,Country1) => ngb(Country,Neighbours), member(Country1,Neighbours). colour_countries(Colours) => % setof([Country,_], X^ngb(Country,X), Colours), % Picat don't have setof/3 or setof/2. findall($c(Country,_), ngb(Country,_)) = Colours, colours(Colours). colours([]) ?=> true. colours(CRest) => CRest = [$c(Country,Colour)|Rest], colours(Rest), member(Colour,[yellow,blue,red,green]), not (member($c(Country1,Colour),Rest), neighbour(Country,Country1)). index (-,-) ngb(austria, [germany,switzerland,italy]). ngb(belgium, [france,germany,netherlands]). ngb(denmark, [germany]). ngb(france, [spain,belgium,switzerland,germany,italy]). ngb(germany, [netherlands,belgium,france,switzerland,austria,denmark]). ngb(italy, [france,switzerland,austria]). ngb(netherlands, [belgium,germany]). ngb(portugal, [spain]). ngb(spain, [portugal,france]). ngb(switzerland, [france,germany,austria,italy]).