/* Reveal the Mapping problem in Picat. From Programming Contest 46 - Reveal the Mapping http://cplus.about.com/od/programmingchallenges/a/Programming-Contest-46-explain-rings.htm """ Every four years a certain set of 5 rings springs to prominence and the circles are featured in this month's contest. There are several answers to the problem. Using just the 9 unique digits 1-9, map them to the unknowns (a-i) in the diagram so that the sum of the unknowns in each circle add up to 11. [ The circles contains the following letters Circle 1: A,B 1,2 Circle 2: B,C,D 2,3,4 Circle 3: D,E,F 4,5,6 Circle 4: F,G,H 6,7,8 Circle 5: H,I 8,9 ] For instance in the first circle a and b must add up to 11. Then b+c+d in the next circle etc and every digit is used only once. This is a brute force speed contest but you must work out all solutions, not just the first. For instance If a= 10 and b= 1 is part of one solution then another might have h= 10 and i = 1 etc. So there must be at least two solutions. Of course A=1 and B= 10 might also work! Thanks to Gary at http://delphiforfun.org who posted this originally. The Contest Look at the rings and the areas occupied by a letter. Each circle has two or three letters. Work out the values of all the letters such that in every circle the total of the letters is 11. Then do it for all possible solutions. Output File Please write the different solutions like this into a results file called results.txt in the same location as your exe: Answer 1 A= value B= C= ... Answer 2 A= value ... Answer 3 etc ... Followed by the average time to solve this. If it's very fast, run it a few thousand or so times so that it takes a few seconds to run. Average time = number of runs/time. Winning the Contest The fastest wins, assuming you get all the answers correct! Timing Code Please time from the start of the run until just before the output file is opened for writing. This code below will do high speed timing for Windows (first three) and the fourth one for Linux. """ There are two (symmetric) solutions: [8,3,7,1,6,4,5,2,9] 1 = [8,3] 2 = [3,7,1] 3 = [1,6,4] 4 = [4,5,2] 5 = [2,9] [9,2,5,4,6,1,7,3,8] 1 = [9,2] 2 = [2,5,4] 3 = [4,6,1] 4 = [1,7,3] 5 = [3,8] This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => Circles = [{1,2}, {2,3,4}, {4,5,6}, {6,7,8}, {8,9}], NumCircles = Circles.len, NumLetters = 9, Total = 11, % decision variables X = new_list(NumLetters), X :: 1..NumLetters, all_different(X), foreach(I in 1..NumCircles) sum([X[J] : J in Circles[I]]) #= Total end, solve(X), println(X), foreach(I in 1..NumCircles) println(I=[X[J] : J in Circles[I]]) end, nl, fail, nl.