aoc2023/day12.pl

85 lines
2.1 KiB
Perl
Raw Normal View History

2023-12-12 14:37:05 -05:00
:- use_module(library(dcg/basics)).
:- use_module(library(dcg/high_order)).
ok([], [], []).
ok([T|Ts], Cs, [0'.|Rs]) :- can_empty(T), ok(Ts, Cs, Rs).
ok(Ts, [C|Cs], Rs) :- fill(Ts, C, Rs, Ts1, Rs1), ok(Ts1, Cs, Rs1).
fill([], 0, [], [], []).
fill([T|Ts], 0, [0'.|Rs], Ts, Rs) :- can_empty(T).
fill([T|Ts], C, [0'#|Rs], Ts1, Rs1) :-
C > 0, C1 is C - 1,
can_fill(T), fill(Ts, C1, Rs, Ts1, Rs1).
can_fill(0'#).
can_fill(0'?).
can_empty(0'.).
can_empty(0'?).
count(T, C, N) :- bagof(S, ok(T, C, S), Ss), length(Ss, N).
count(T-C, N) :- count(T, C, N).
examples1 :-
count(`???.###`, [1, 1, 3], 1),
count(`.??..??...?##.`, [1, 1, 3], 4),
count(`?#?#?#?#?#?#?#?`, [1, 3, 1, 6], 1),
count(`????.#...#...`, [4, 1, 1], 1),
count(`????.######..#####.`, [1, 6, 5], 4),
count(`?###????????`, [3, 2, 1], 10).
file(Lines) --> sequence(line, Lines).
line(T-C) --> template(T), " ", clues(C), blanks.
template(T) --> sequence(tchar, T).
tchar(T) --> [T], {member(T, `?#.`)}.
clues(C) --> sequence(number, ",", C).
part1(File) :-
phrase_from_file(file(TCs), File), !,
maplist(count, TCs, Ns),
foldl(plus, Ns, 0, N),
writeln(N).
/*
% oof ouch my stack
count_unfold(T-C, N) :-
unfold_template(T, T1),
unfold_clues(C, C1),
count(T1-C1, N).
unfold_template(T, R) :- unfold_template(5, T, R).
unfold_template(1, T, T) :- !.
unfold_template(N, T, R) :-
N > 1, N1 is N - 1,
unfold_template(N1, T, R1),
append(T, [0'?|R1], R).
unfold_clues(C, R) :- unfold_clues(5, C, R).
unfold_clues(1, C, C) :- !.
unfold_clues(N, C, R) :-
N > 1, N1 is N - 1,
unfold_clues(N1, C, R1),
append(C, R1, R).
examples2 :-
count_unfold(`???.###`-[1,1,3], 1),
count_unfold(`.??..??...?##.`-[1,1,3], 16384),
count_unfold(`?#?#?#?#?#?#?#?`-[1,3,1,6], 1),
count_unfold(`????.#...#...`-[4,1,1], 16),
count_unfold(`????.######..#####.`-[1,6,5], 2500),
count_unfold(`?###????????`-[3,2,1], 506250).
part2(File) :-
phrase_from_file(file(TCs), File), !,
maplist(count_unfold, TCs, Ns),
foldl(plus, Ns, 0, N),
writeln(N).
*/