:- use_module(library(pio)). :- use_module(library(dcg/basics)). :- initialization(main, main). main([FileName|_]) :- input(FileName, Hails), maplist(xyline, Hails, Lines), findall( [N1, N2, X, Y], ( member(N1-L1-X1assert, Lines), member(N2-L2-X2assert, Lines), N1 < N2, intersect(L1, L2, X-Y), call(X1assert, X), call(X2assert, X), X >= 200000000000000, X =< 400000000000000, Y >= 200000000000000, Y =< 400000000000000), Intersects), length(Intersects, Answer), write(Answer), nl. intersect(A1-B1-C1, A2-B2-C2, X-Y) :- Det is A1*B2 - A2*B1, Det =\= 0, X is (B2*C1 - B1*C2) / Det, Y is (-A2*C1 + A1*C2) / Det. xyline(N-(X-Y-_-Dx-Dy-_), N-(A-B-C)-Xassert) :- A = Dy, B is -Dx, C is X*Dy - Y*Dx, ( Dx >= 0 -> Xassert = =<(X); Xassert = >=(X) ). % input parsing stuff below. Brick indexing is for debugging. % assumption: no same hail. There are parallels, but no same input(FileName, Hails) :- phrase_from_file(hails(0, Hails), FileName). hails(_, []) --> eos, !. hails(N, [N-(X-Y-Z-Dx-Dy-Dz)|Hails]) --> blanks, number(X), ",", blanks, number(Y), ",", blanks, number(Z), " @", blanks, number(Dx), ",", blanks, number(Dy), ",", blanks, number(Dz), "\n", {NextN is N + 1}, hails(NextN, Hails).