From a437ae1e1c4c8ceb5b86b3c84e575d6cda0590bc Mon Sep 17 00:00:00 2001 From: Dory Date: Mon, 11 Dec 2023 19:14:46 -0800 Subject: [PATCH] cleanup d11 code --- 11/{part1.pl => day11.pl} | 32 ++++++++++++++++++-------------- 11/part2.pl | 37 ------------------------------------- 2 files changed, 18 insertions(+), 51 deletions(-) rename 11/{part1.pl => day11.pl} (50%) delete mode 100644 11/part2.pl diff --git a/11/part1.pl b/11/day11.pl similarity index 50% rename from 11/part1.pl rename to 11/day11.pl index 4082368..3c1038e 100644 --- a/11/part1.pl +++ b/11/day11.pl @@ -1,28 +1,31 @@ +% usase: swipl day11.pl input.txt 1000000 :- use_module(library(pio)). :- initialization(main, main). -main([FileName | _]) :- +main([FileName, ExpandFactorArg | _]) :- input(FileName, Map), - findall(Dist, distance(_, Map, _, Dist), Dists), + atom_number(ExpandFactorArg, ExpandFactor), + findall(Dist, distance(_, Map, _, ExpandFactor, Dist), Dists), sum_list(Dists, Sum), writef('Answer=%t\n', [Sum]). % Dist is distance along Axis in Map between two row/col A & B (after expansion) -distance(Axis, Map, A-B, Dist) :- - true_coords(Axis, Map, TrueNs), +distance(Axis, Map, A-B, ExpandFactor, Dist) :- + true_coords(Axis, Map, ExpandFactor, TrueNs), member(A-ACount, TrueNs), member(B-BCount, TrueNs), A < B, Dist is (B - A) * ACount * BCount. -% TrueXs is the list of all true coordinates along Axis in Map -true_coords(x, Map, TrueXs) :- - NextX = [Row, LastX-_, X-Count]>>( +% TrueXs is the list of all coordinates after expanion along Axis in Map +true_coords(x, Map, ExpandFactor, TrueXs) :- + NextX = {ExpandFactor}/[Row, LastX-_, X-Count]>>( sum_list(Row, Count), - (Count = 0 -> X is LastX + 2; X is LastX + 1)), + (Count = 0 -> X is LastX + ExpandFactor; X is LastX + 1)), scanl(NextX, Map, -1-0, Xs), Xs = [_ | TrueXs]. -true_coords(y, [FirstRow | Map], TrueYs) :- + +true_coords(y, [FirstRow | Map], ExpandFactor, TrueYs) :- foldl(add_vectors, Map, FirstRow, CountY), - NextY = [Count, LastY-_, Y-Count]>>( - Count = 0 -> Y is LastY + 2; Y is LastY + 1), + NextY = {ExpandFactor}/[Count, LastY-_, Y-Count]>>( + Count = 0 -> Y is LastY + ExpandFactor; Y is LastY + 1), scanl(NextY, CountY, -1-0, Ys), Ys = [_ | TrueYs]. add_vectors([], [], []). @@ -30,8 +33,9 @@ add_vectors([A | V1], [B | V2], [C | V]) :- C is A + B, add_vectors(V1, V2, V). % Read file into 2D array Map. 1 corresponding to a galaxy and 0 otherwise. input(FileName, Map) :- phrase_from_file(lines(Map), FileName). -lines([]) --> eos, !. +lines([]) --> eos. lines([Line|Lines]) --> line(Line), lines(Lines). -line([]) --> ( "\n" ; eos ), !. -line([L|Ls]) --> ([C], {C =:= 35 -> L = 1; L = 0}), line(Ls). +line([]) --> "\n" ; eos. +line([1|Ls]) --> "#", line(Ls). +line([0|Ls]) --> ".", line(Ls). eos([], []). diff --git a/11/part2.pl b/11/part2.pl deleted file mode 100644 index 4653bdc..0000000 --- a/11/part2.pl +++ /dev/null @@ -1,37 +0,0 @@ -:- use_module(library(pio)). -:- initialization(main, main). - -main([FileName | _]) :- - input(FileName, Map), - findall(Dist, distance(_, Map, _, Dist), Dists), - sum_list(Dists, Sum), - writef('Answer=%t\n', [Sum]). - -% Dist is distance along Axis in Map between two row/col A & B (after expansion) -distance(Axis, Map, A-B, Dist) :- - true_coords(Axis, Map, TrueNs), - member(A-ACount, TrueNs), member(B-BCount, TrueNs), A < B, - Dist is (B - A) * ACount * BCount. - -% TrueXs is the list of all true coordinates along Axis in Map -true_coords(x, Map, TrueXs) :- - NextX = [Row, LastX-_, X-Count]>>( - sum_list(Row, Count), - (Count = 0 -> X is LastX + 1000000; X is LastX + 1)), - scanl(NextX, Map, -1-0, Xs), Xs = [_ | TrueXs]. -true_coords(y, [FirstRow | Map], TrueYs) :- - foldl(add_vectors, Map, FirstRow, CountY), - NextY = [Count, LastY-_, Y-Count]>>( - Count = 0 -> Y is LastY + 1000000; Y is LastY + 1), - scanl(NextY, CountY, -1-0, Ys), Ys = [_ | TrueYs]. - -add_vectors([], [], []). -add_vectors([A | V1], [B | V2], [C | V]) :- C is A + B, add_vectors(V1, V2, V). - -% Read file into 2D array Map. 1 corresponding to a galaxy and 0 otherwise. -input(FileName, Map) :- phrase_from_file(lines(Map), FileName). -lines([]) --> eos, !. -lines([Line|Lines]) --> line(Line), lines(Lines). -line([]) --> ( "\n" ; eos ), !. -line([L|Ls]) --> ([C], {C =:= 35 -> L = 1; L = 0}), line(Ls). -eos([], []).