-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.pl
30 lines (25 loc) · 944 Bytes
/
12.pl
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
:- table dp(_,_,sum).
main :-
read_string(user_input, _, S), split_string(S, "\n", "\n", Lines),
maplist([Line, L-Cs]>>(
split_string(Line, " ", "", [Left, CSS]), string_codes(Left, L),
split_string(CSS, ",", "", CsL), maplist(number_string, Cs, CsL)
), Lines, Input),
aggregate_all(sum(R), (member(L-Cs, Input), solve(L, Cs, R)), Part1),
format("Part 1: ~d~n", [Part1]),
aggregate_all(sum(R), (
member(L-Cs, Input),
append([L, [0'?], L, [0'?], L, [0'?], L, [0'?], L], NewL),
append([Cs, Cs, Cs, Cs, Cs], NewCs),
solve(NewL, NewCs, R)
), Part2),
format("Part 2: ~d~n", [Part2]).
solve(L, Cs, R) :- append(L, [0'.], I), dp(I, Cs, R).
dp([], [], 1).
dp([], [_|_], 0).
dp([A|L], Cs, R) :- A \= 0'#, dp(L, Cs, R).
dp([A|L], [C|Cs], R) :- A \= 0'.,
length(Head, C),
append(Head, [CT|Tail], [A|L]),
CT \= 0'#, \+ member(0'., Head),
dp(Tail, Cs, R).