/* Palindrome days in Picat. Inpired by Tias Guns' Palindrome Days model in CPMpy: https://github.com/CPMpy/cpmpy/blob/master/examples/palindrome_days.py """ how many 'palindrome days' are there in a century? for example, today: 121121 (12 november 2021) Thanks for asking Michiel : ) """ * go/0 includes leap years (and thus century), and a little more general handling of the date formats: yymmdd, ddmmyy, and mmddyy. yymmdd and ddmmyy has both 30 palindromic dates, though they are not the same. mmddyy has only 24 palindromic days. format = yymmdd [011110,021120,031130,101101,111111,12121,201102,211112,221122,301103,311113,321123,401104,411114,421124,501105,511115,521125,601106,611116,621126,701107,711117,721127,801108,811118,821128,901109,911119,921129] len = 30 format = ddmmyy [011110,021120,031130,041140,051150,061160,071170,081180,091190,101101,111111,121121,131131,141141,151151,161161,171171,181181,191191,201102,211112,221122,231132,241142,251152,261162,271172,281182,291192,301103] len = 30 format = mmddyy [011110,012210,021120,022220,031130,032230,041140,042240,051150,052250,061160,062260,071170,072270,081180,082280,091190,092290,101101,102201,111111,112211,121121,122221] len = 24 2021-11-13: The next palindromic date is 22-11-22 (2022-11-22). * go2/0: Testing the date format YYYYMMDD (which I would like to call "proper" palindromic date). As of writing (the day after 21-11-12), the next "proper" palindromic date is 20211202 (2021-12-02) There is one palindromic date with a leap day: 9220-02-29 This model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp,util. main => go. go ?=> member(Century,[1900,2000,2900]), println(century=Century), Formats = [yymmdd,ddmmyy,mmddyy], foreach(Format in Formats) println(format=Format), All=findall(V,palindrome_days(Century,Format,V)), println([[D.to_string() : D in V].join('') : V in All.sort()]), println(len=All.len), nl end, fail, nl. go => true. /* Here are all the palindromic YYYYMMDD dates for centuries 1000..9999. Almost all have 12 palindromic dates, except for century 1300 which have only 7. century = 1000 [10011001,10100101,10111101,10200201,10211201,10300301,10400401,10500501,10600601,10700701,10800801,10900901] len = 12 century = 1100 [11011011,11100111,11111111,11200211,11211211,11300311,11400411,11500511,11600611,11700711,11800811,11900911] len = 12 century = 1200 [12011021,12100121,12111121,12200221,12211221,12300321,12400421,12500521,12600621,12700721,12800821,12900921] len = 12 century = 1300 [13011031,13100131,13211231,13300331,13500531,13700731,13800831] len = 7 century = 2000 [20011002,20100102,20111102,20200202,20211202,20300302,20400402,20500502,20600602,20700702,20800802,20900902] len = 12 century = 2100 [21011012,21100112,21111112,21200212,21211212,21300312,21400412,21500512,21600612,21700712,21800812,21900912] len = 12 century = 2200 [22011022,22100122,22111122,22200222,22211222,22300322,22400422,22500522,22600622,22700722,22800822,22900922] len = 12 century = 3000 [30011003,30100103,30111103,30200203,30211203,30300303,30400403,30500503,30600603,30700703,30800803,30900903] len = 12 century = 3100 [31011013,31100113,31111113,31200213,31211213,31300313,31400413,31500513,31600613,31700713,31800813,31900913] len = 12 century = 3200 [32011023,32100123,32111123,32200223,32211223,32300323,32400423,32500523,32600623,32700723,32800823,32900923] len = 12 century = 4000 [40011004,40100104,40111104,40200204,40211204,40300304,40400404,40500504,40600604,40700704,40800804,40900904] len = 12 century = 4100 [41011014,41100114,41111114,41200214,41211214,41300314,41400414,41500514,41600614,41700714,41800814,41900914] len = 12 century = 4200 [42011024,42100124,42111124,42200224,42211224,42300324,42400424,42500524,42600624,42700724,42800824,42900924] len = 12 century = 5000 [50011005,50100105,50111105,50200205,50211205,50300305,50400405,50500505,50600605,50700705,50800805,50900905] len = 12 century = 5100 [51011015,51100115,51111115,51200215,51211215,51300315,51400415,51500515,51600615,51700715,51800815,51900915] len = 12 century = 5200 [52011025,52100125,52111125,52200225,52211225,52300325,52400425,52500525,52600625,52700725,52800825,52900925] len = 12 century = 6000 [60011006,60100106,60111106,60200206,60211206,60300306,60400406,60500506,60600606,60700706,60800806,60900906] len = 12 century = 6100 [61011016,61100116,61111116,61200216,61211216,61300316,61400416,61500516,61600616,61700716,61800816,61900916] len = 12 century = 6200 [62011026,62100126,62111126,62200226,62211226,62300326,62400426,62500526,62600626,62700726,62800826,62900926] len = 12 century = 7000 [70011007,70100107,70111107,70200207,70211207,70300307,70400407,70500507,70600607,70700707,70800807,70900907] len = 12 century = 7100 [71011017,71100117,71111117,71200217,71211217,71300317,71400417,71500517,71600617,71700717,71800817,71900917] len = 12 century = 7200 [72011027,72100127,72111127,72200227,72211227,72300327,72400427,72500527,72600627,72700727,72800827,72900927] len = 12 century = 8000 [80011008,80100108,80111108,80200208,80211208,80300308,80400408,80500508,80600608,80700708,80800808,80900908] len = 12 century = 8100 [81011018,81100118,81111118,81200218,81211218,81300318,81400418,81500518,81600618,81700718,81800818,81900918] len = 12 century = 8200 [82011028,82100128,82111128,82200228,82211228,82300328,82400428,82500528,82600628,82700728,82800828,82900928] len = 12 century = 9000 [90011009,90100109,90111109,90200209,90211209,90300309,90400409,90500509,90600609,90700709,90800809,90900909] len = 12 century = 9100 [91011019,91100119,91111119,91200219,91211219,91300319,91400419,91500519,91600619,91700719,91800819,91900919] len = 12 century = 9200 [92011029,92100129,92111129,92200229,92211229,92300329,92400429,92500529,92600629,92700729,92800829,92900929] len = 12 */ go2 ?=> member(C,10..99), Century = C*100, All=findall(V,palindrome_days(Century,yyyymmdd,V)), All.len > 0, println(century=Century), println([[D.to_string() : D in V].join('') : V in All.sort()]), println(len=All.len), nl, fail, nl. go2 => true. % % We assume that Format contains % YY, MM, and DD % in some permutation or % YYYYMMDD % palindrome_days(Century,Format,V) => CenturyList = Century.to_string.map(to_int), FormatString = Format.to_string(), Len = FormatString.len, V = new_list(Len), V :: 0..9, Day :: 1..31, Month :: 1..12, V #= V.reverse(), % palindrome % Extract the format if Len == 8 then Year :: 0..9999, find(FormatString,"yyyy",Y1,Y2), Year #= 1000*V[Y1] + 100*V[Y1+1] + 10*V[Y1+2] + V[Y2], CenturyList[1] #= V[Y1], CenturyList[2] #= V[Y1+1], YearYYYY #= Year else Year :: 0..99, find(FormatString,"yy",Y1,Y2), Year #= 10*V[Y1] + V[Y2], YearYYYY #= 1000*CenturyList[1] + 100*CenturyList[2] + 10*V[Y1] + V[Y2] end, find(FormatString,"mm",M1,M2), find(FormatString,"dd",D1,D2), Month #= 10*V[M1] + V[M2], Day #= 10*V[D1] + V[D2], leap_year(YearYYYY,LeapYear), (Month #= 2 #/\ LeapYear #= 1) #=> Day #<= 29, % February is a leap year (Month #= 2 #/\ LeapYear #= 0) #=> Day #<= 28, % February is not a leap year foreach(No31 in [4,6,9,11]) Month #= No31 #=> Day #<= 30 end, solve(V ++ [LeapYear]). % % B = 1 if Year is a leap year, else 0 % leap_year(Year, B) => B :: 0..1, ( (Year mod 4 #= 0 #/\ Year mod 100 #!= 0) #\/ (Year mod 400 #= 0) ) #<=> B #= 1.