-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.pl
54 lines (37 loc) · 1.09 KB
/
lib.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
:- use_module(library(pio)).
% parsing
rules([]) --> call(eos), !.
rules([R|Rs]) --> rule(R), rules(Rs).
eos([], []).
rule((Pl, Pw)) --> policy_string(Pl), ": ", letters(Pw), ( "\n" ; call(eos) ).
policy_string(policy(Min, Max, Letter)) --> integer(Min), "-", integer(Max), " ", letter(Letter).
integer(I) -->
digit(D0),
digits(D),
{ number_codes(I, [D0|D]) }.
digits([D|T]) --> digit(D), !, digits(T).
digits([]) --> [].
digit(D) --> [D], { code_type(D, digit) }.
letters([L|Ls]) --> letter(L), !, letters(Ls).
letters([]) --> [].
letter(C) --> [X], { code_type(X, alpha) }, { char_code(C, X) }.
% policy
get_policy_first(policy(M, _, _), M).
get_policy_second(policy(_, M, _), M).
get_policy_letter(policy(_, _, L), L).
% count valid passwords
count_valids([], 0).
count_valids([(Pl, Pw)|T], N0) :-
valid(Pl, Pw),
count_valids(T, N1),
N0 is N1+1.
count_valids([(Pl, Pw)|T], N0) :-
not(valid(Pl, Pw)),
count_valids(T, N0).
% output
show_valids :-
phrase_from_file(rules(Rs), 'input.txt'),
count_valids(Rs, C),
write(C), nl,
fail.
show_valids.