-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.pl
More file actions
executable file
·35 lines (27 loc) · 931 Bytes
/
Copy pathday2.pl
File metadata and controls
executable file
·35 lines (27 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env swipl -O -t main --quiet
:- consult(shared).
rle([], []).
rle([C], [[C]]).
rle([C | X], [[C] | Y]) :- X = [C2 | _], C \= C2, rle(X, Y).
rle([C | X], [[C | D] | Rest]) :- X = [C | _], rle(X, [D | Rest]).
process(S, R) :- string_chars(S, C), msort(C, L), rle(L, R).
double([[X, X] | _]).
double([_ | R]) :- double(R).
triple([[X, X, X] | _]).
triple([_ | R]) :- triple(R).
pair([X|Xs], (X, Y)) :- member(Y, Xs).
pair([_|Xs], P) :- pair(Xs, P).
pair([], _) :- false.
similar(([X|Xs], [Y|Xs])) :- dif(X, Y).
similar(([X|Xs], [X|Ys])) :- similar((Xs, Ys)).
main :-
input(Lines),
maplist(process, Lines, Entries),
include(double, Entries, Doubles), length(Doubles, D),
include(triple, Entries, Triples), length(Triples, T),
Result is D * T,
writeln(Result),
maplist(string_chars, Lines, Words),
findall(P, pair(Words, P), Pairs),
include(similar, Pairs, MatchingPairs),
writeln(MatchingPairs).