day 3 prolog

This commit is contained in:
chayleaf 2021-12-09 01:41:52 +07:00
parent 3955e35520
commit 78f3d2c5d0
2 changed files with 152 additions and 0 deletions

63
pl/03.1.pl Normal file
View file

@ -0,0 +1,63 @@
chars_digits([], []).
chars_digits([C|Cs], [D|Ds]) :-
char_type(C, digit(D)),
chars_digits(Cs, Ds).
read_file(Stream, []) :-
at_end_of_stream(Stream).
read_file(Stream, [X|L]) :-
\+ at_end_of_stream(Stream),
read_line_to_codes(Stream, C),
chars_digits(C, X),
read_file(Stream, L).
main :-
open('../input/03', read, Stream),
read_file(Stream, Lines), !,
close(Stream),
solve(Lines, A, B),
bin_dec(A, A1),
bin_dec(B, B1),
X is A1 * B1,
print([A1, B1, X]).
list_sum([], [], []).
list_sum([A|As], [B|Bs], [C|Cs]) :-
list_sum(As, Bs, Cs),
C is A + B.
listlist_sum([], C, C).
listlist_sum([A|As], I, C) :-
list_sum(A, I, I1),
listlist_sum(As, I1, C).
zerolist([], []).
zerolist([_|As], [0|Bs]) :-
zerolist(As, Bs).
listlist_sum([A|As], Sum) :-
zerolist(A, B),
listlist_sum([A|As], B, Sum).
div([], _, []).
div([A|As], N, [B|Bs]) :-
B is A // N,
div(As, N, Bs).
neg([], []).
neg([A|As], [B|Bs]) :-
B is 1 - A,
neg(As, Bs).
solve(Lines, Res1, Res2) :-
listlist_sum(Lines, Sum),
length(Lines, Len),
div(Sum, Len // 2, Res1),
neg(Res1, Res2).
bin_dec(B, X) :-
bin_dec(B, 0, X).
bin_dec([], X, X).
bin_dec([B|Bs], N, X) :-
M is N * 2 + B,
bin_dec(Bs, M, X).

89
pl/03.2.pl Normal file
View file

@ -0,0 +1,89 @@
chars_digits([], []).
chars_digits([C|Cs], [D|Ds]) :-
char_type(C, digit(D)),
chars_digits(Cs, Ds).
read_file(Stream, []) :-
at_end_of_stream(Stream).
read_file(Stream, [X|L]) :-
\+ at_end_of_stream(Stream),
read_line_to_codes(Stream, C),
chars_digits(C, X),
read_file(Stream, L).
main :-
open('../input/03', read, Stream),
read_file(Stream, Lines), !,
close(Stream),
most_solve(Lines, A),
least_solve(Lines, B),
bin_dec(A, A1),
bin_dec(B, B1),
X is A1 * B1,
print([A1, B1, X]).
first_elements([], []).
first_elements([[X|_]|As], [B|Bs]) :-
first_elements(As, Bs),
B is X.
last_elements([], _, []).
last_elements([[X|Xs]|As], X, [Xs|Bs]) :-
last_elements(As, X, Bs).
last_elements([[X|_]|As], F, Bs) :-
F =\= X,
last_elements(As, F, Bs).
freq([], 0, 0).
freq([1|As], Z, O) :-
freq(As, Z, O1),
O is O1 + 1.
freq([0|As], Z, O) :-
freq(As, Z1, O),
Z is Z1 + 1.
most_common(A, 1) :-
freq(A, Z, O),
O >= Z.
most_common(A, 0) :-
freq(A, Z, O),
O < Z.
least_common(A, 0) :-
freq(A, Z, O),
Z > 0,
(O >= Z; O = 0).
least_common(A, 1) :-
freq(A, Z, O),
O > 0,
(O < Z; Z = 0).
first_elem([A|_], A).
most_solve(A, [B]) :-
first_elements(A, F),
most_common(F, B),
first_elem(A, N),
length(N, 1).
most_solve(A, [B|Bs]) :-
first_elements(A, F),
most_common(F, B),
last_elements(A, B, L),
most_solve(L, Bs).
least_solve(A, [B]) :-
first_elements(A, F),
least_common(F, B),
first_elem(A, N),
length(N, 1).
least_solve(A, [B|Bs]) :-
first_elements(A, F),
least_common(F, B),
last_elements(A, B, L),
least_solve(L, Bs).
bin_dec(B, X) :-
bin_dec(B, 0, X).
bin_dec([], X, X).
bin_dec([B|Bs], N, X) :-
M is N * 2 + B,
bin_dec(Bs, M, X).