Skip to content

Instantly share code, notes, and snippets.

@sudhackar

sudhackar/91316.pl Secret

Last active Feb 8, 2020
Embed
What would you like to do?
puzzling se
:- use_module(library(clpfd)).
list_allperms(L, Ps) :- bagof(P, permutation(L,P), Ps).
f(X,Y,Z,R) :- R #= 10 * X + 11 * Y + 12 * Z.
fit(X,Y,Z) :- f(X,Y,Z,R), R #< 51.
fit_limit(L) :- nth0(0,L,X),nth0(1,L,Y),nth0(2,L,Z),fit(X,Y,Z).
fit_unique(L,R) :- nth0(0,L,X),nth0(1,L,Y),nth0(2,L,Z),f(X,Y,Z,R).
solution(X,Y,Z) :-
X #>=0,
Y #>=0,
Z #>=0,
list_allperms([X,Y,Z],L),
maplist(fit_limit, L),
maplist(fit_unique,L,R),
all_distinct(R),
label([X,Y,Z]),
write(R).
:- use_module(library(clpfd)).
fit(X) :- X #>= 1, X #=< 7 .
n_fit(L,V) :- nth0(X1,L,V),nth0(Y1,L,V),X1 #\= Y1, abs(X1-Y1) #= (V+1).
n_six_fit(L,Base,Idx,Val) :- Y #= Base+Idx, nth0(Y,L,X), X #= Val.
count_fit(L) :- maplist(n_fit(L),[1,2,3,4,5,6,7]).
six_fit(L) :- nth0(X6,L,6),nth0(Y6,L,6), Y6 #> X6, Y6 #=< 13, X6 #=< 6,
maplist(n_six_fit(L,X6),[1,2,3,4,5,6],N),
is_set(N).
num([],0).
num([L|Ns],X) :- num(Ns,Y),length(Ns,P),X is L*(10**P) + Y.
solution(Num) :-
maplist(fit,[M,N,O,P,Q,R,S,T,U,V,W,X,Y,S]),
count_fit([M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z]),
six_fit([M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z]),
num([M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z],Num).
?- distinct(X,solution(X)).
:- use_module(library(clpfd)).
sudoku(Rows) :-
length(Rows, 9), maplist(same_length(Rows), Rows),
append(Rows, Vs), Vs ins 1..9,
maplist(all_distinct, Rows),
transpose(Rows, Columns),
maplist(all_distinct, Columns),
Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is],
blocks(As, Bs, Cs),
blocks(Ds, Es, Fs),
blocks(Gs, Hs, Is),
nth0(0,As,D1),nth0(1,Bs,D2),nth0(2,Cs,D3),nth0(3,Ds,D4),nth0(4,Es,D5),nth0(5,Fs,D6),nth0(6,Gs,D7),nth0(7,Hs,D8),nth0(8,Is,D9),
nth0(8,As,E1),nth0(7,Bs,E2),nth0(6,Cs,E3),nth0(5,Ds,E4),nth0(4,Es,E5),nth0(3,Fs,E6),nth0(2,Gs,E7),nth0(1,Hs,E8),nth0(0,Is,E9),
nth0(1,Bs,F1),nth0(4,Bs,F2),nth0(7,Bs,F3),nth0(1,Es,F4),nth0(4,Es,F5),nth0(7,Es,F6),nth0(1,Hs,F7),nth0(4,Hs,F8),nth0(7,Hs,F9),
all_distinct([D1,D2,D3,D4,D5,D6,D7,D8,D9]),
all_distinct([E1,E2,E3,E4,E5,E6,E7,E8,E9]),
all_distinct([F1,F2,F3,F4,F5,F6,F7,F8,F9]),
nth0(3,As,K1_1),nth0(3,Cs,K1_2),nth0(0,Ds,K1_3),nth0(2,Ds,K1_4),K1_1 #\= F1,K1_2 #\= F1,K1_3 #\= F1,K1_4 #\= F1,
nth0(2,As,K2_1),nth0(2,Cs,K2_2),nth0(6,As,K2_3),nth0(6,Cs,K2_4),nth0(3,Ds,K2_5),nth0(5,Ds,K2_6),K2_1 #\= F2,K2_2 #\= F2,K2_3 #\= F2,K2_4 #\= F2,K2_5 #\= F2,K2_6 #\= F2,
nth0(6,As,K3_1),nth0(6,Cs,K3_2),nth0(6,Ds,K3_3),nth0(8,Ds,K3_4),K3_1 #\= F3,K3_2 #\= F3,K3_3 #\= F3,K3_4 #\= F3,
nth0(3,Ds,K4_1),nth0(3,Fs,K4_2),nth0(0,Gs,K4_3),nth0(2,Gs,K4_4),nth0(0,Cs,K4_5),nth0(2,Cs,K4_6),K4_1 #\= F4,K4_2 #\= F4,K4_3 #\= F4,K4_4 #\= F4,K4_5 #\= F4,K4_6 #\= F4,
nth0(3,Cs,K5_1),nth0(5,Cs,K5_2),nth0(2,Ds,K5_3),nth0(2,Fs,K5_4),nth0(6,Ds,K5_5),nth0(6,Fs,K5_6),nth0(3,Gs,K5_7),nth0(5,Fs,K5_8),K5_1 #\= F5,K5_2 #\= F5,K5_3 #\= F5,K5_4 #\= F5,K5_5 #\= F5,K5_6 #\= F5,K5_7 #\= F5,K5_8 #\= F5,
nth0(5,Ds,K6_1),nth0(5,Fs,K6_2),nth0(6,Gs,K6_3),nth0(8,Gs,K6_4),nth0(6,Cs,K6_5),nth0(8,Cs,K6_6),K6_1 #\= F6,K6_2 #\= F6,K6_3 #\= F6,K6_4 #\= F6,K6_5 #\= F6,K6_6 #\= F6,
nth0(0,Fs,K7_1),nth0(2,Fs,K7_2),nth0(3,Is,K7_3),nth0(3,Gs,K7_4),K7_1 #\= F7,K7_2 #\= F7,K7_3 #\= F7,K7_4 #\= F7,
nth0(2,Gs,K8_1),nth0(2,Is,K8_2),nth0(6,Gs,K8_3),nth0(6,Is,K8_4),nth0(3,Fs,K8_5),nth0(5,Fs,K8_6),K8_1 #\= F8,K8_2 #\= F8,K8_3 #\= F8,K8_4 #\= F8,K8_5 #\= F8,K8_6 #\= F8,
nth0(8,Fs,K9_1),nth0(6,Fs,K9_2),nth0(5,Gs,K9_3),nth0(5,Is,K9_4),K9_1 #\= F9,K9_2 #\= F9,K9_3 #\= F9,K9_4 #\= F9.
blocks([], [], []).
blocks([N1,N2,N3|Ns1], [N4,N5,N6|Ns2], [N7,N8,N9|Ns3]) :-
all_distinct([N1,N2,N3,N4,N5,N6,N7,N8,N9]),
blocks(Ns1, Ns2, Ns3).
problem(1, [[8,2,1,4,7,9,_,_,6],
[_,9,4,_,_,3,_,_,_],
[_,_,3,_,_,_,9,1,_],
[_,_,_,_,2,_,_,_,9],
[_,_,9,3,_,_,_,_,_],
[_,_,_,_,9,_,_,_,_],
[_,_,_,9,_,7,_,_,_],
[9,_,_,6,3,_,4,_,_],
[3,_,_,_,_,_,1,9,_]]).
:- use_module(library(clpfd)).
sudoku(Rows) :-
length(Rows, 9), maplist(same_length(Rows), Rows),
append(Rows, Vs), Vs ins 1..9,
maplist(all_distinct, Rows),
transpose(Rows, Columns),
maplist(all_distinct, Columns),
Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is],
blocks(As, Bs, Cs),
blocks(Ds, Es, Fs),
blocks(Gs, Hs, Is),
nth0(0,As,D1),nth0(1,Bs,D2),nth0(2,Cs,D3),nth0(3,Ds,D4),nth0(4,Es,D5),nth0(5,Fs,D6),nth0(6,Gs,D7),nth0(7,Hs,D8),nth0(8,Is,D9),
nth0(8,As,E1),nth0(7,Bs,E2),nth0(6,Cs,E3),nth0(5,Ds,E4),nth0(4,Es,E5),nth0(3,Fs,E6),nth0(2,Gs,E7),nth0(1,Hs,E8),nth0(0,Is,E9),
nth0(1,Bs,F1),nth0(4,Bs,F2),nth0(7,Bs,F3),nth0(1,Es,F4),nth0(4,Es,F5),nth0(7,Es,F6),nth0(1,Hs,F7),nth0(4,Hs,F8),nth0(7,Hs,F9),
all_distinct([D1,D2,D3,D4,D5,D6,D7,D8,D9]),
all_distinct([E1,E2,E3,E4,E5,E6,E7,E8,E9]),
all_distinct([F1,F2,F3,F4,F5,F6,F7,F8,F9]),
F3 #< F2,
F2 #< F1,
F1 #< F4,
F4 #< F7,
F7 #< F8,
F8 #< F9,
% F9 #< F6,
F6 #< F3.
% write(done).
blocks([], [], []).
blocks([N1,N2,N3|Ns1], [N4,N5,N6|Ns2], [N7,N8,N9|Ns3]) :-
all_distinct([N1,N2,N3,N4,N5,N6,N7,N8,N9]),
blocks(Ns1, Ns2, Ns3).
problem(1, [[5,7,2,_,_,9,_,3,1],
[6,4,8,_,3,1,_,2,_],
[9,1,3,_,_,_,_,_,_],
[_,3,7,1,5,_,_,_,_],
[_,5,6,_,9,_,3,1,8],
[8,9,1,3,_,6,_,7,5],
[1,2,5,9,_,_,7,6,3],
[3,6,9,_,7,_,1,8,4],
[7,8,4,6,1,3,_,_,2]]).
from random import randint, sample
from collections import defaultdict
def cals():
start_set = set()
start_set.add(1)
buf = ""
rank = {}
rank[1] = 1
# rank[0] = 2
while len(start_set) < 100:
a = sample(start_set, 1)
b = sample(start_set, 1)
# print(a,b)
# if a[0] > 5000 or b[0] > 5000:
# continue
if 2020 in start_set:
break
start_set.add(a[0] + b[0])
start_set.add(abs(a[0] - b[0]))
start_set.add(a[0] * b[0])
buf += "%d + %d = %d\n%d - %d = %d\n%d * %d = %d\n" % (a[0],b[0],a[0]+b[0],a[0],b[0],abs(a[0]-b[0]),a[0],b[0],a[0]*b[0])
if (a[0] + b[0]) not in rank:
rank[a[0] + b[0]] = max(rank[a[0]],rank[b[0]]) + 1
if (a[0] * b[0]) not in rank:
rank[a[0] * b[0]] = max(rank[a[0]],rank[b[0]]) + 1
if abs(a[0] - b[0]) not in rank:
rank[abs(a[0] - b[0])] = max(rank[a[0]],rank[b[0]]) + 1
if 2020 in start_set:
# print(start_set)
# print(buf)
# print(rank[2020])
return (rank[2020],buf)
else:
return (None,None)
while True:
x,y = cals()
if x:
print(x)
if x < 8:
print(y)
:- use_module(library(clpfd)).
forward(you, 240).
forward(jacky, 360).
forward(chris, 420).
forward(martha, 600).
backward(you, 120).
backward(jacky, 60).
backward(chris, 45).
backward(martha, 30).
moves(Ms) :- phrase(moves(state(0,[you,jacky,chris,martha],[])), Ms).
moves(state(T0,Ls0,Rs0)) -->
{ select(Player1, Ls0, Ls1), select(Player2, Ls1, Ls2),
Player1 @< Player2,
forward(Player1, Time1), forward(Player2, Time2),
T1 #= T0 + max(Time1,Time2), T1 #< 1500 },
[together(Player1,Player2,T1)],
moves_(state(T1,Ls2,[Player1,Player2|Rs0])).
moves_(state(_,[],_)) --> [].
moves_(state(T0,Ls0,Rs0)) -->
{ select(Player, Rs0, Rs1),
backward(Player, Time),
T1 #= T0 + Time, T1 #< 1500 },
[alone(Player,T1)],
moves(state(T1,[Player|Ls0],Rs1)).
from graphviz import Graph
from fractions import gcd
p1 = [9, 15, 21, 10, 7, 70]
G1 = Graph('G1', filename="Graph1")
edges = []
for i in p1:
for j in p1:
if i != j and gcd(i, j) > 1:
edges.append((i, j))
edges = set([tuple(sorted(i)) for i in edges])
for edge in edges:
G1.edge(str(edge[0]), str(edge[1]))
G1.view()
G1.save("G1.dot")
p2 = [9, 42, 21, 20, 35, 5]
G2 = Graph('G2', filename="Graph2")
edges = []
for i in p2:
for j in p2:
if i != j and gcd(i, j) > 1:
edges.append((i, j))
edges = set([tuple(sorted(i)) for i in edges])
for edge in edges:
G2.edge(str(edge[0]), str(edge[1]))
G2.view()
G2.save("G2.dot")
:- use_module(library(clpfd)).
median(Row, X) :-
msort(Row, Sorted),
nth0(1, Sorted, X).
solution(Rows) :-
length(Rows, 3),
maplist(same_length(Rows), Rows),
append(Rows, Vs),
permutation(Vs, [1, 2, 3, 4, 5, 6, 7, 8, 9]),
transpose(Rows, Columns),
maplist(median, Rows, RMedians),
maplist(median, Columns, CMedians),
append([RMedians, CMedians], AllMedians),
all_distinct(AllMedians), writeln(Rows), writeln(CMedians), writeln(RMedians).
solve(1, [[_, _, _], [_, _, _], [_, _, _]]).
?-solve(1, X), solution(X), maplist(portray_clause, X).
:- use_rendering(table,
[header(h('Name','Surname','L1','L2'))]).
interpreters(Is) :-
length(Is,6),
member(h(fran,french,_,_), Is),
member(h(geraldine,german,_,_), Is),
member(h(dudley,dutch,_,_), Is),
member(h(spike,spanish,_,_), Is),
member(h(polly,polish,_,_), Is),
member(h(ron,romanian,_,_), Is),
member(h(_,_,french,_), Is),member(h(_,_,_,french), Is),
member(h(_,_,german,_), Is),member(h(_,_,_,german), Is),
member(h(_,_,dutch,_), Is),member(h(_,_,_,dutch), Is),
member(h(_,_,spanish,_), Is),member(h(_,_,_,spanish), Is),
member(h(_,_,polish,_), Is),member(h(_,_,_,polish), Is),
member(h(_,_,romanian,_), Is),member(h(_,_,_,romanian), Is),
\+member(h(_,X,X,_), Is),
\+member(h(_,Y,_,Y), Is),
(member(h(spike,spanish,dutch,german), Is);member(h(spike,spanish,german,dutch), Is)),
(member(h(_,_,dutch,polish), Is);member(h(_,_,polish,dutch), Is)),
member(h(fran,french,E,F), Is),member(h(dudley,dutch,G,H), Is),E \= G, F \= G, E \= H, F \= H,
member(h(dudley,dutch,I,J), Is), (member(h(_,I,_,french), Is);member(h(_,I,french,_), Is)), (member(h(_,J,_,french), Is);member(h(_,J,french,_), Is)),
\+member(h(_,_,german,polish), Is),
\+member(h(_,_,polish,german), Is),
\+member(h(_,_,K,K), Is).
:- use_module(library(clpfd)).
% Of the shrine in Uchida and the temple built in 1645, one is sama takako and the other is okabe honzo.
% The temple in Funai was built before takahashi
% The temple in toyagi was built 120 years before the temple in usui
% Hori takesi wasa built after sama takako
temples(Ts):-
length(Ts,4),
member(h(hori_takesi, _, _), Ts),
member(h(okabe_honzo, _, _), Ts),
member(h(sama_takako, _, _), Ts),
member(h(takahashi, _, _), Ts),
member(h(_, funai, _), Ts),
member(h(_, toyagi, _), Ts),
member(h(_, uchida, _), Ts),
member(h(_, usui, _), Ts),
member(h(_, _, 1525), Ts),
member(h(_, _, 1585), Ts),
member(h(_, _, 1645), Ts),
member(h(_, _, 1705), Ts),
member(h(takahashi, _, X), Ts), member(h(_, funai, Y), Ts), Y #< X,
member(h(_, toyagi, Z), Ts), member(h(_, usui, W), Ts), Z + 120 #= W,
member(h(sama_takako, _, U), Ts), member(h(hori_takesi, _, V), Ts), V #> U,
(member(h(sama_takako,uchida,_), Ts),member(h(okabe_honzo,_,1645), Ts),\+member(h(okabe_honzo,uchida,_),Ts),\+member(h(sama_takako,_,1645), Ts));(\+member(h(sama_takako,uchida,_), Ts),\+member(h(okabe_honzo,_,1645), Ts),member(h(okabe_honzo,uchida,_),Ts),member(h(sama_takako,_,1645), Ts)).
day(X) :- member(X,[m,t,w,thu,f,sat,sun]).
lie(m).
lie(t).
lie(w).
truth(X) :- \+lie(X).
next(A, B, Ls) :- append(_, [A,B|_], Ls).
next_day(sun,m).
next_day(X,Y) :- next(X,Y,[m,t,w,thu,f,sat,sun]).
solve(X) :-
day(X),
(truth(X),next_day(Y,X),lie(Y),next_day(X,T),next_day(T,U),next_day(U,V),lie(U),lie(V));
(lie(X),next_day(Y,X),truth(Y),next_day(X,T),next_day(T,U),next_day(U,V),(truth(U);truth(V))).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.