view 3-Prolog/day1-map-colouring.pl @ 73:74976fddd25f

Ignore Erlang binary files and remove existing ones
author IBBoard <dev@ibboard.co.uk>
date Sat, 03 Feb 2018 19:52:45 +0000
parents 005ae3fad18f
children
line wrap: on
line source

% Colouring is done by defining facts of what colours must be "different"
% i.e. not adjacent.
% Note that we define all combinations - reflexivity isn't assumed, because
% these are facts and not rules.
different(red, green). different(red, blue).
different(green, red). different(green, blue).
different(blue, red). different(blue, green).

% We then use a rule with variables to see which facts fit for the variables
% Note: With this approach, we need to manually define that each state must be
% different from each other state. We've not actually encoded any concept of
% adjacency, just a request for differences.
% On the plus side, we only need to define it in one direction, because the
% facts already cover reflexivity.
colouring(Alabama, Mississipi, Georgia, Tennessee, Florida) :-
    different(Mississipi, Tennessee),
    different(Mississipi, Alabama),
    different(Alabama, Tennessee),
    different(Alabama, Mississipi),
    different(Alabama, Georgia),
    different(Alabama, Florida),
    different(Georgia, Florida),
    different(Georgia, Tennessee).
% The bad side with this is that we need to call
% "colouring(Alabama, Mississipi, Georgia, Tennessee, Florida)." to run it.
% On the minus side, that seems like duplication. On the plus side, we could
% technically use any variable name and apply it to any situation where
% adjacency is the same, it's just that the example wanted to be explicit.