Compare commits
3 commits
6c72571acf
...
e019bfc085
Author | SHA1 | Date | |
---|---|---|---|
chayleaf | e019bfc085 | ||
chayleaf | 5b5f21ce4a | ||
chayleaf | b69b03fc31 |
44
pl/06.1.pl
Normal file
44
pl/06.1.pl
Normal file
|
@ -0,0 +1,44 @@
|
|||
numbers_strings([], []).
|
||||
numbers_strings([A|As], [B|Bs]) :-
|
||||
number_string(A, B),
|
||||
numbers_strings(As, Bs).
|
||||
|
||||
read_file(Stream, X) :-
|
||||
\+ at_end_of_stream(Stream),
|
||||
read_line_to_codes(Stream, C),
|
||||
string_chars(S, C),
|
||||
split_string(S, ",", "", Y),
|
||||
numbers_strings(X, Y),
|
||||
at_end_of_stream(Stream).
|
||||
|
||||
main :-
|
||||
open('../input/06', read, Stream),
|
||||
read_file(Stream, Lines), !,
|
||||
close(Stream),
|
||||
iterate_n(256, Lines, Res),
|
||||
length(Res, N),
|
||||
print(N).
|
||||
|
||||
tick_existing([], [], 0).
|
||||
tick_existing([0|Xs], [6|Ys], New) :-
|
||||
tick_existing(Xs, Ys, New1),
|
||||
New is New1 + 1.
|
||||
tick_existing([X|Xs], [Y|Ys], New) :-
|
||||
X > 0,
|
||||
tick_existing(Xs, Ys, New),
|
||||
Y is X - 1.
|
||||
spawn_n(0, []).
|
||||
spawn_n(N, [8|Ys]) :-
|
||||
N1 is N - 1,
|
||||
spawn_n(N1, Ys).
|
||||
iterate(X, Y) :-
|
||||
tick_existing(X, Y0, New), !,
|
||||
spawn_n(New, Y1),
|
||||
append(Y0, Y1, Y), !.
|
||||
|
||||
iterate_n(0, X, X).
|
||||
iterate_n(N, X, Y) :-
|
||||
N1 is N - 1,
|
||||
iterate(X, Y0), !,
|
||||
iterate_n(N1, Y0, Y), !.
|
||||
|
53
pl/06.2.pl
Normal file
53
pl/06.2.pl
Normal file
|
@ -0,0 +1,53 @@
|
|||
numbers_strings([], []).
|
||||
numbers_strings([A|As], [B|Bs]) :-
|
||||
number_string(A, B),
|
||||
numbers_strings(As, Bs).
|
||||
|
||||
read_file(Stream, X) :-
|
||||
\+ at_end_of_stream(Stream),
|
||||
read_line_to_codes(Stream, C),
|
||||
string_chars(S, C),
|
||||
split_string(S, ",", "", Y),
|
||||
numbers_strings(X, Y),
|
||||
at_end_of_stream(Stream).
|
||||
|
||||
main :-
|
||||
open('../input/06', read, Stream),
|
||||
read_file(Stream, Lines), !,
|
||||
close(Stream),
|
||||
create_buckets(B0),
|
||||
add_to_bucket_seq(B0, Lines, B),
|
||||
%add_to_bucket_seq(B0, [3,4,3,1,2], B),
|
||||
iterate_n(256, B, B1),
|
||||
sum_list(B1, S),
|
||||
print([B1,S]).
|
||||
|
||||
create_buckets([0,0,0,0,0,0,0,0,0]).
|
||||
|
||||
prefix(0, _, []).
|
||||
prefix(N, [A|As], [A|Ps]) :-
|
||||
N > 0,
|
||||
N1 is N - 1,
|
||||
prefix(N1, As, Ps).
|
||||
|
||||
add_to_bucket(A, N, B) :-
|
||||
prefix(N, A, P),
|
||||
append([P, [X], S], A),
|
||||
X1 is X + 1,
|
||||
append([P, [X1], S], B).
|
||||
|
||||
add_to_bucket_seq(A, [], A).
|
||||
add_to_bucket_seq(A, [N|Ns], B) :-
|
||||
add_to_bucket(A, N, B0),
|
||||
add_to_bucket_seq(B0, Ns, B).
|
||||
|
||||
iterate([A0,A1,A2,A3,A4,A5,A6,A7,A8], [A1,A2,A3,A4,A5,A6,B,A8,A0]) :-
|
||||
B is A0 + A7.
|
||||
|
||||
iterate_n(0, A, A).
|
||||
iterate_n(N, A, B) :-
|
||||
N > 0,
|
||||
iterate(A, B0),
|
||||
N1 is N - 1,
|
||||
iterate_n(N1, B0, B).
|
||||
|
28
pl/07.1.pl
Normal file
28
pl/07.1.pl
Normal file
|
@ -0,0 +1,28 @@
|
|||
numbers_strings([], []).
|
||||
numbers_strings([A|As], [B|Bs]) :-
|
||||
number_string(A, B),
|
||||
numbers_strings(As, Bs).
|
||||
|
||||
read_file(Stream, X) :-
|
||||
\+ at_end_of_stream(Stream),
|
||||
read_line_to_codes(Stream, C),
|
||||
string_chars(S, C),
|
||||
split_string(S, ",", "", Y),
|
||||
numbers_strings(X, Y),
|
||||
at_end_of_stream(Stream).
|
||||
|
||||
main :-
|
||||
open('../input/07', read, Stream),
|
||||
read_file(Stream, Lines), !,
|
||||
close(Stream),
|
||||
msort(Lines, L),
|
||||
length(L, Len),
|
||||
I is Len // 2,
|
||||
nth0(I, L, X),
|
||||
calculate_diff(L, X, N),
|
||||
print([X, N]).
|
||||
|
||||
calculate_diff([], _, 0).
|
||||
calculate_diff([A|As], X, B) :-
|
||||
calculate_diff(As, X, B0),
|
||||
B is B0 + abs(A - X).
|
41
pl/07.2.pl
Normal file
41
pl/07.2.pl
Normal file
|
@ -0,0 +1,41 @@
|
|||
numbers_strings([], []).
|
||||
numbers_strings([A|As], [B|Bs]) :-
|
||||
number_string(A, B),
|
||||
numbers_strings(As, Bs).
|
||||
|
||||
read_file(Stream, X) :-
|
||||
\+ at_end_of_stream(Stream),
|
||||
read_line_to_codes(Stream, C),
|
||||
string_chars(S, C),
|
||||
split_string(S, ",", "", Y),
|
||||
numbers_strings(X, Y),
|
||||
at_end_of_stream(Stream).
|
||||
|
||||
main :-
|
||||
open('../input/07', read, Stream),
|
||||
read_file(Stream, Lines), !,
|
||||
close(Stream),
|
||||
msort(Lines, L),
|
||||
min_list(L, A),
|
||||
max_list(L, B),
|
||||
bin_search(A, B, L, N),
|
||||
print(N).
|
||||
|
||||
tri(A, B) :-
|
||||
B is (A * (A + 1)) // 2.
|
||||
|
||||
calculate_diff([], _, 0).
|
||||
calculate_diff([A|As], X, B) :-
|
||||
calculate_diff(As, X, B0),
|
||||
tri(abs(A - X), T),
|
||||
B is B0 + T.
|
||||
|
||||
bin_search(L, L, A, Ans) :-
|
||||
calculate_diff(A, L, Ans).
|
||||
bin_search(L, R, A, Ans) :-
|
||||
L < R,
|
||||
M is (L + R) // 2,
|
||||
((M >= R, A1 is 9999999999999999); bin_search(L, M, A, A1)),
|
||||
((M =< L, A2 is 9999999999999999); bin_search(M, R, A, A2)),
|
||||
Ans is min(A1, A2).
|
||||
|
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