day 8 prolog
This commit is contained in:
parent
5b5f21ce4a
commit
e019bfc085
89
pl/08.1.pl
Normal file
89
pl/08.1.pl
Normal file
|
@ -0,0 +1,89 @@
|
|||
remove_last(A, C) :-
|
||||
reverse(A, [_|B]),
|
||||
reverse(B, C).
|
||||
|
||||
strings_chars([], []).
|
||||
strings_chars([A|As], [B|Bs]) :-
|
||||
string_chars(A, B),
|
||||
strings_chars(As, Bs).
|
||||
|
||||
read_file(Stream, []) :-
|
||||
at_end_of_stream(Stream).
|
||||
read_file(Stream, [X|Xs]) :-
|
||||
\+ at_end_of_stream(Stream),
|
||||
read_line_to_codes(Stream, C),
|
||||
string_chars(S, C),
|
||||
split_string(S, "|", "", [A, B]),
|
||||
split_string(A, " ", "", A0),
|
||||
split_string(B, " ", "", [_|B0]),
|
||||
remove_last(A0, A1),
|
||||
strings_chars(A1, A2),
|
||||
strings_chars(B0, B1),
|
||||
X = [A2, B1],
|
||||
% X = S,
|
||||
read_file(Stream, Xs).
|
||||
|
||||
main :-
|
||||
open('../input/08', read, Stream),
|
||||
read_file(Stream, Lines), !,
|
||||
close(Stream),
|
||||
solve_all(Lines, Ans),
|
||||
count_all(Ans, S),
|
||||
print(S).
|
||||
|
||||
solve_all([], []).
|
||||
solve_all([L|Ls], [A|As]) :-
|
||||
solve(L, A),
|
||||
solve_all(Ls, As).
|
||||
|
||||
should_count(1).
|
||||
should_count(4).
|
||||
should_count(7).
|
||||
should_count(8).
|
||||
|
||||
count([], 0).
|
||||
count([N|Ns], S) :-
|
||||
should_count(N),
|
||||
count(Ns, S1),
|
||||
S is S1 + 1.
|
||||
count([N|Ns], S) :-
|
||||
\+ should_count(N),
|
||||
count(Ns, S).
|
||||
|
||||
count_all([], 0).
|
||||
count_all([N|Ns], S) :-
|
||||
count(N, S0),
|
||||
count_all(Ns, S1),
|
||||
S is S0 + S1.
|
||||
|
||||
letter(a).
|
||||
letter(b).
|
||||
letter(c).
|
||||
letter(d).
|
||||
letter(e).
|
||||
letter(f).
|
||||
letter(g).
|
||||
|
||||
solve([Q, W], X) :-
|
||||
letter(A), letter(B), letter(C), letter(D), letter(E), letter(F), letter(G),
|
||||
A \= B, A \= C, A \= D, A \= E, A \= F, A \= G,
|
||||
B \= C, B \= D, B \= E, B \= F, B \= G,
|
||||
C \= D, C \= E, C \= F, C \= G,
|
||||
D \= E, D \= F, D \= G,
|
||||
E \= F, E \= G,
|
||||
F \= G,
|
||||
length(A1, 2), length(A7, 3), length(A4, 4), length(A8, 7),
|
||||
permutation([_, A1, _, _, A4, _, _, A7, A8, _], Q), !,
|
||||
decode_all(W, X, [A1, A4, A7, A8]).
|
||||
|
||||
decode_all([], [], _).
|
||||
decode_all([Q|Qs], [W|Ws], A) :-
|
||||
decode(Q, W, A),
|
||||
decode_all(Qs, Ws, A).
|
||||
|
||||
decode(Q, W, [A1, A4, A7, A8]) :-
|
||||
((permutation(A1, Q), W is 1, !);
|
||||
(permutation(A4, Q), W is 4, !);
|
||||
(permutation(A7, Q), W is 7, !);
|
||||
(permutation(A8, Q), W is 8, !);
|
||||
(W is -1)).
|
92
pl/08.2.pl
Normal file
92
pl/08.2.pl
Normal file
|
@ -0,0 +1,92 @@
|
|||
remove_last(A, C) :-
|
||||
reverse(A, [_|B]),
|
||||
reverse(B, C).
|
||||
|
||||
strings_chars([], []).
|
||||
strings_chars([A|As], [B|Bs]) :-
|
||||
string_chars(A, B),
|
||||
strings_chars(As, Bs).
|
||||
|
||||
read_file(Stream, []) :-
|
||||
at_end_of_stream(Stream).
|
||||
read_file(Stream, [X|Xs]) :-
|
||||
\+ at_end_of_stream(Stream),
|
||||
read_line_to_codes(Stream, C),
|
||||
string_chars(S, C),
|
||||
split_string(S, "|", "", [A, B]),
|
||||
split_string(A, " ", "", A0),
|
||||
split_string(B, " ", "", [_|B0]),
|
||||
remove_last(A0, A1),
|
||||
strings_chars(A1, A2),
|
||||
strings_chars(B0, B1),
|
||||
X = [A2, B1],
|
||||
% X = S,
|
||||
read_file(Stream, Xs).
|
||||
|
||||
main :-
|
||||
open('../input/08', read, Stream),
|
||||
read_file(Stream, Lines), !,
|
||||
close(Stream),
|
||||
solve_all(Lines, Ans),
|
||||
print(Ans).
|
||||
|
||||
solve_all([], 0).
|
||||
solve_all([L|Ls], S) :-
|
||||
solve(L, A),
|
||||
solve_all(Ls, S0),
|
||||
S is S0 + A.
|
||||
|
||||
dig_dec(A, N) :-
|
||||
dig_dec(A, 0, N).
|
||||
dig_dec([], N, N).
|
||||
dig_dec([A|As], N, C) :-
|
||||
C0 is N * 10 + A,
|
||||
dig_dec(As, C0, C).
|
||||
|
||||
letter(a).
|
||||
letter(b).
|
||||
letter(c).
|
||||
letter(d).
|
||||
letter(e).
|
||||
letter(f).
|
||||
letter(g).
|
||||
|
||||
solve([Q, W], X) :-
|
||||
letter(A), letter(B), letter(C), letter(D), letter(E), letter(F), letter(G),
|
||||
A \= B, A \= C, A \= D, A \= E, A \= F, A \= G,
|
||||
B \= C, B \= D, B \= E, B \= F, B \= G,
|
||||
C \= D, C \= E, C \= F, C \= G,
|
||||
D \= E, D \= F, D \= G,
|
||||
E \= F, E \= G,
|
||||
F \= G,
|
||||
length(A1, 2), length(A7, 3), length(A4, 4), length(A2, 5), length(A3, 5), length(A5, 5), length(A0, 6), length(A6, 6), length(A9, 6), length(A8, 7),
|
||||
permutation([A0, A1, A2, A3, A4, A5, A6, A7, A8, A9], Q),
|
||||
permutation([C, F], A1),
|
||||
permutation([A, C, F], A7),
|
||||
permutation([B, C, D, F], A4),
|
||||
permutation([A, B, C, D, E, F, G], A8),
|
||||
permutation([A, B, D, F, G], A5),
|
||||
permutation([A, C, D, F, G], A3),
|
||||
permutation([A, C, D, E, G], A2),
|
||||
permutation([A, B, C, E, F, G], A0),
|
||||
permutation([A, B, D, E, F, G], A6),
|
||||
permutation([A, B, C, D, F, G], A9), !,
|
||||
decode_all(W, Y, [A0, A1, A2, A3, A4, A5, A6, A7, A8, A9]),
|
||||
dig_dec(Y, X).
|
||||
|
||||
decode_all([], [], _).
|
||||
decode_all([Q|Qs], [W|Ws], A) :-
|
||||
decode(Q, W, A),
|
||||
decode_all(Qs, Ws, A).
|
||||
|
||||
decode(Q, W, [A0, A1, A2, A3, A4, A5, A6, A7, A8, A9]) :-
|
||||
((permutation(A0, Q), W is 0);
|
||||
(permutation(A1, Q), W is 1);
|
||||
(permutation(A2, Q), W is 2);
|
||||
(permutation(A3, Q), W is 3);
|
||||
(permutation(A4, Q), W is 4);
|
||||
(permutation(A5, Q), W is 5);
|
||||
(permutation(A6, Q), W is 6);
|
||||
(permutation(A7, Q), W is 7);
|
||||
(permutation(A8, Q), W is 8);
|
||||
(permutation(A9, Q), W is 9)), !.
|
Loading…
Reference in a new issue