/* Car Talk Odometer problem in Picat. From http://www.cartalk.com/content/tommy%E2%80%99s-drive-work?question """ ... RAY: Well, not exactly new, it's 19 years old. But it's new enough to have lots of things that his other car didn't have. It has heat, lights, and windshield wipers. It also has one of those newfangled six-digit odometers. It can register as many as 999,999 miles. So one fine morning last week, Tommy gets into his new car to drive to work. He fires up his engine and before pulling out of the driveway, he notices something interesting. His odometer reading is a palindrome. Do you know what a palindrome is? TOM: No. RAY: It's a number that reads the same forwards as it does backwards. For example, if his odometer read 175,571 miles, that's a palindrome number. 1 - 7 - 5, 5 - 7 -1, reads the same forward and backwards. 'Well, that's cool,' he says to himself, and off he goes. Naturally, he's in no hurry to get to work, so he stops at his favorite cafe and gets his usual quadruple espresso macchiato, and about an hour later, he shows up at Car Talk Plaza. Just as he pulls into his 'Reserved for the Big Cheese' parking spot, he notices that his odometer reading again is a palindrome. And no, the odometer is not broken, but it is indeed a palindrome number once again. So he's gotten in the morning, seen a number, driven some number of miles - not many, cause he only drove for an hour,--not even, most of the time he was drinking coffee -- and his new odometer reading is again a palindromic number. Here's the question: How far did he drive that morning? """ Answer: http://www.cartalk.com/content/tommy%E2%80%99s-drive-work?answer This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => N = 6, Start :: 100000..999999, End :: 100000..999999, StartA = new_list(N), StartA :: 0..9, EndA = new_list(N), EndA :: 0..9, Z #= End - Start, Z #> 0, to_num(StartA,10,Start), to_num(EndA,10,End), is_palindromic(StartA), is_palindromic(EndA), Vars = StartA ++ EndA ++ [Start,End,Z], solve($[min(Z),ff,split],Vars), println('start'=Start), println('end '=End), println('z '=Z), nl. to_num(List, Base, Num) => Len = length(List), Num #= sum([List[I]*Base**(Len-I) : I in 1..Len]). % % Ensure that t is a palindromic number. % (We assume that the are no leasing 0's.) % is_palindromic(A) => Len = A.len, foreach(I in 1..Len) A[I] #= A[Len-I+1] end.