Courses/CS 460/Fall 2005/Homework/Joey Leung/Nov 5

From CSWiki

Jump to: navigation, search

[edit] LET+THERE+BE=LIGHT


local
   proc {Light Solution}
      L E T H R B I G
      Vars = [L E T H R B I G]
   in
      Solution = [[L E T]#[T H E R E]#[B E]#[L I G H T]]
      Vars ::: 0#9
      {FD.distinct Vars}
      L \=: 0
      T \=: 0
      B \=: 0
                            100*L + 10*E + T
      +  10000*T + 1000*H + 100*E + 10*R + E
      +                             10*B + E
      =: 10000*L + 1000*I + 100*G + 10*H + T
      {FD.distribute ff Vars}
   end
in
   {Browse {SearchAll Light}}
end



[edit] Rotating Digits


local
   % got this proc {DigitsToInt L} from Brian Smith
   fun {DigitsToInt L}
     {FoldL L fun {$ I J} {FD.plus {FD.times I 10} J} end 0}
   end

   proc {Mult D1 X D2}
      {DigitsToInt D1} * X =: {DigitsToInt D2}
   end
      
   proc {RotatingDigits Solution}
      A B C D E F
      Vars = [A B C D E F]
   in
      Solution = Vars
      Vars ::: 1#9
      {Mult Vars 1 [A B C D E F]}
      {Mult Vars 2 [C D E F A B]}
      {Mult Vars 3 [B C D E F A]}
      {Mult Vars 4 [E F A B C D]}
      {Mult Vars 5 [F A B C D E]}
      {Mult Vars 6 [D E F A B C]}
      {FD.distribute ff Vars}
      {FD.distinct Vars}
   end
in
   {Browse {SearchAll RotatingDigits}}
end




[edit] Divisible digits

Does not work. The program took a very long time and return a "nil".



local
   % got this proc {DigitsToInt L} from Brian Smith
   fun {DigitsToInt L}
      {FoldL L fun {$ I J} {FD.plus {FD.times I 10} J} end 0}
   end

   proc {Divisible Solution}
      N0 N1 N2 N3 N4 N5 N6 N7 N8 N9  
      Vars = [N0 N1 N2 N3 N4 N5 N6 N7 N8 N9]
   in
      Solution = Vars
      Vars ::: 0#9
      {FD.modI {DigitsToInt Vars} 9} =:0
      {FD.modI {DigitsToInt Vars} 8} =:0
      {FD.modI {DigitsToInt Vars} 7} =:0
      {FD.modI {DigitsToInt Vars} 6} =:0
      {FD.modI {DigitsToInt Vars} 5} =:0
      {FD.distribute ff Vars}
      {FD.distinct Vars}
   end
in
   {Browse {SearchAll Divisible}}
end