day 6 prolog
This commit is contained in:
parent
6c72571acf
commit
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).
|
||||||
|
|
Loading…
Reference in a new issue