From 460a9a2fbf6c0b0ebfb424bc3c598bc663b3b011 Mon Sep 17 00:00:00 2001 From: rhiannon morris Date: Thu, 14 Dec 2023 19:45:59 +0100 Subject: [PATCH] day12.2 is still too hungy --- day12.pl | 69 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/day12.pl b/day12.pl index 8fb4668..4c5f2d3 100644 --- a/day12.pl +++ b/day12.pl @@ -1,15 +1,28 @@ :- 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). +:- table ok/3. +ok([], [], []). +ok([0'.|Ts], Cs, [0'.|Rs]) :- + ok(Ts, Cs, Rs). +ok([0'#|Ts], [C|Cs], [0'#|Rs]) :- + C1 is C - 1, + fill(Ts, C1, Rs, Ts1, Rs1), + ok(Ts1, Cs, Rs1). +ok([0'?|Ts], Cs, [0'.|Rs]) :- + ok(Ts, Cs, Rs). +ok([0'?|Ts], [C|Cs], [0'#|Rs]) :- + C1 is C - 1, + fill(Ts, C1, Rs, Ts1, Rs1), + ok(Ts1, Cs, Rs1). -fill([], 0, [], [], []). -fill([T|Ts], 0, [0'.|Rs], Ts, Rs) :- can_empty(T). + +:- table fill/5. +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(T), !, fill(Ts, C1, Rs, Ts1, Rs1). can_fill(0'#). can_fill(0'?). @@ -45,12 +58,12 @@ part1(File) :- /* -% oof ouch my stack ☹ +% oof ouch my memory ☹ -count_unfold(T-C, N) :- +count_unfold(T, C, N) :- unfold_template(T, T1), unfold_clues(C, C1), - count(T1-C1, N). + count(T1, C1, N). unfold_template(T, R) :- unfold_template(5, T, R). unfold_template(1, T, T) :- !. @@ -67,18 +80,42 @@ unfold_clues(N, C, R) :- 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). + 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), + abolish_all_tables. +count_unfold_all(Ls, Ns) :- + length(Ls, Len), + count_unfold_all(1, Len, Ls, Ns). +count_unfold_all(_, _, [], []) :- !, abolish_all_tables. +count_unfold_all(I, Len, [T0-C0|Ls], [N|Ns]) :- + abolish_all_tables, + unfold_template(T0, T), unfold_clues(C0, C), + length(T, LenT), length(C, LenC), wildcards(T, W), + format("[~d/~d] ~w ~w ~w > ", [I, Len, 'T'(LenT), 'C'(LenC), 'W'(W)]), flush, + !, call_time(count(T, C, N), Time), !, + format("[~2fs] ~d~n", [Time.wall, N]), + I1 is I + 1, + count_unfold_all(I1, Len, Ls, Ns). + +wildcards(T, N) :- + setof(I, nth0(I, T, 0'?), Is), length(Is, N). + part2(File) :- + abolish_all_tables, + writeln(go), + set_prolog_flag(stack_limit, 18_000_000_000), + set_prolog_flag(table_space, 40_000_000_000), phrase_from_file(file(TCs), File), !, - maplist(count_unfold, TCs, Ns), + count_unfold_all(TCs, Ns), foldl(plus, Ns, 0, N), writeln(N). */ + +% vim: set ft=prolog :