/* The Yoga Class Puzzle in Picat. From Rajul Saxena "The Yoga Class Puzzle" https://medium.com/puzzle-sphere/yoga-class-word-puzzle-math-logic-apple-google-amazon-microsoft-faang-quantitative-aptitude-calendar-date-dfcc31bf32c6 """ Two friends, Ekko and Jinx both decided to join a community yoga class in the new year. The yoga class takes place everyday. Both have different schedules and attend the class accordingly. Ekko started attending the class on the first Monday in January. After that, Ekko attended every fifth day. Jinx started attending the class on the first Tuesday in January. After that, Jinx attended every fourth day. Ekko and Jinx attended the class on the same day only once in January. On which day of the month did Ekko and Jinx attend the class together? Helpful Hint: January has 31 days. """ first = 2 = tue ef = 7 jfa = 1 e = [0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0] j = [1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0] Meeting day: Jan 17 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. import cp. main => go. /* CP version, which I used to think about the problem */ go ?=> nolog, Days = 31, % January has 31 days First :: 1..7, % The first weekday of this specific January is a 1:Monday .. 7:Sunday % Ekko E = new_list(Days), E :: 0..1, % Jinx J = new_list(Days), J :: 0..1, % Ekko started attending the class on the first Monday in January. % After that, Ekko attended every fifth day. EF #= 1+((8 - First) mod 7), foreach(D in 1..Days) (D-EF) mod 5 #= 0 #<=> E[D] #= 1 end, % Jinx started attending the class on the first Tuesday in January. % After that, Jinx attended every fourth day. JF #= 1+((8 - First + 1) mod 7), foreach(D in 1..Days) (D-JF) mod 4 #= 0 #<=> J[D] #= 1 end, % Ekko and Jinx attended the class on the same day only once in January. sum([E[I]*J[I] #= 1 : I in 1..Days]) #= 1, Vars = E ++ J ++ [First,EF,JF], solve($[],Vars), Ds = new_map([1=mon,2=tue,3=wed,4=thu,5=fri,6=sat,7=sun]), println(first=First=Ds.get(First)), println(ef=EF), println(jf=JF), println(e=E), println(j=J), printf("Meeting day: Jan %d\n",[D : D in 1..Days, E[D] == 1, J[D] == 1].first), nl, fail, nl. go => true. /* This is a much simpler approach (written after solving it with go/0). Meeting day: Jan 17 (first day is 2, a tuesday) */ go2 ?=> member(First,1..7), EF = 1 + ((8-First) mod 7), JF = 1 + ((8-First+1) mod 7), X = [[D,First] : D in 1..31, (D-EF) mod 5 == 0, (D-JF) mod 4 == 0], X.len == 1, Ds = new_map([1=mon,2=tues,3=wednes,4=thurs,5=fri,6=satur,7=sun]), printf("Meeting day: Jan %d (first day is %d, a %wday)\n", X[1,1], X[1,2],Ds.get(X[1,2])), nl.