changeset 53:005ae3fad18f

Write up the map colouring code
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Sep 2017 19:57:50 +0100
parents cbaa3546f3f8
children 91d02b3d74f8
files 3-Prolog/day1-map-colouring.pl 3-Prolog/readme.txt
diffstat 2 files changed, 29 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/3-Prolog/day1-map-colouring.pl	Tue Sep 26 19:57:50 2017 +0100
@@ -0,0 +1,28 @@
+% 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.
--- a/3-Prolog/readme.txt	Tue Sep 26 19:42:02 2017 +0100
+++ b/3-Prolog/readme.txt	Tue Sep 26 19:57:50 2017 +0100
@@ -9,6 +9,6 @@
 returns "yes" or "no" at a prompt when it has finished.
 
 When you ask a question with a placeholder, the placeholder has to be capitalised. Lower-case makes it an atom.
-When Prolog returns its answers, type ";" to get the next response. Sometimes Prolog will end multiple responses
+When Prolog returns its answers, type ";" to get the next response or "a" to get all. Sometimes Prolog will end multiple responses
 with "no", sometimes with "yes". This depends on whether it needs to do more calculations to check for more answers ("no")
 or whether it knows it successfully gave you everything ("yes").
\ No newline at end of file