-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing_files.pl
94 lines (78 loc) · 2.91 KB
/
parsing_files.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
zone(L, C, Z) :- Z is (1 + floor((C - 1)/3) + 3 * floor((L - 1)/3)).
%% i(): true if the 2nd parameter contains the list of all
%% integers contained in the string (passed as ASCII list)
%% passed as 1st parameter
% example usage: i("1 2 3", [49, 50, 51]).
% another example usage: i("1 _ 3", [49, 51]).
digit_code(J) :- J >= 0'0, J =< 0'9.
i([], []).
i([], _) :- fail.
i([A|S], [B|R]) :- (digit_code(A), B=A; (A=0'_, B is 48)),!, i(S, R). % if the char is not a number but is a "_", we evaluate it to 0 (48)
i([_|S], R) :- i(S, R).
%% transf(): true if the 2nd parameter is the list of integers of the 1st parameter subtracted 48
%% used to transform a list of digits ASCII codes into a list of those digits...
% example usage: transf([0,0], [-48, -48]).
% another example usage: transf([48, 48], L). L=[0,0]
transf([], []).
transf([A|S], [B|R]) :- B is A-48, transf(S, R).
% example usage: super_i("1 _ 3", [1, 3]).
% another example usage: i("1 2 3", [1, 2, 3]).
super_i(Sentence, R) :- i(Sentence, A), transf(A, R).
grid([], _).
% grid(L, R).
read_lines(T) :-
open('./grille.txt', read, F),!, read_lines(T, F), close(F).
read_lines(T, F) :-
read_line_to_codes(F, Temp),
(Temp=end_of_file, T = [] ;
read_lines(T2, F),
super_i(Temp, H),
append(H, T2, T)
)
% to "return" an empty value in the case we reach the end of file, instead of having it un-instantiated
.
transf2([], _).
transf2(G, R) :-
transf2(G, 1, R)
.
transf2([], _, []).
transf2([0|G], N, R) :-
N2 is N+1,
transf2(G, N2, R)
.
transf2([A|G], N, [v(p(L,C,Z), A)|R]) :-
C is mod(N, 9), % col does not go after 9
L is floor(N/9)+1, % line is floor(col/9)+1
zone(L, C, Z),
N2 is N+1,
transf2(G, N2, R)
.
%% Somewhere remplis(1, 1, Grille), transf2(Cases, Grille, Remplies, Restantes) %%, complete(Remplies, Couleurs, RestantesBrute)
% transf2([], _, [], []).
% transf2([0|CS], [G|GS], D, [G|TS]) :-
% transf2(CS, GS, D, TS).
% transf2([C|CS], [G|GS], [v(G, C)|D], TS) :-
% transf2(CS, GS, D, TS).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% prettyprint prints to the output in an elegant way %%
prettyprint(_, []). % End of the print
prettyprint(F, [v(p(_, 9, _), V)|N]) :- write(F, V), nl(F), prettyprint(F, N).
prettyprint(F, [v(_, V)|N]):- write(F, V), write(F, ' '), prettyprint(F, N).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
test(0, _).
test(N, [Nam|FR]) :-
%trace,
number_codes(N, Codes),
append("grilles/", Codes, Name_),
append(Name_, ".txt", Na),
string_to_list(Nam, Na),
open(Nam, read, F),
read_lines(L, F),
close(F),
!,
transf2(L, A), !,
N2 is N-1,
test(N2, FR)
.
% @TODO: find a way to get the line as an ASCII list (with read() ? read_lines_to_codes() ? ) and then use super_i()
% @TODO: Then, use the same way remplis() does to generate a grid (zone(X, Y, X), v(p(X, Y, Z), Value))