-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolve.pl
52 lines (41 loc) · 1.41 KB
/
solve.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
use_module(library(apply)).
use_module(library(lists)).
5BAE
use_module(library(pcre)).
read_lines(L) :-
current_input(In),
read_line_to_string(In, Line),
read_more(Line, L).
read_more(end_of_file, []) :- !.
read_more(Last, [Last|Tail]) :- read_lines(Tail).
parse_record(Line, Rec) :-
re_matchsub("(\\d+)-(\\d+)\s+(.): ([a-z]+)$", Line,
re_match{0: _, 1: AS, 2: BS, 3: LetterS, 4: PasswordS}, []
),
string_to_list(LetterS, [Letter]),
string_to_list(PasswordS, Password),
number_string(A, AS),
number_string(B, BS),
Rec = (A, B, Letter, Password).
is_valid_1(Min, Max, Letter, [Letter|T]) :-
!,
compare(>, Max, 0),
Min2 is max(Min-1, 0),
Max2 is max(Max-1, 0),
is_valid_1(Min2, Max2, Letter, T).
is_valid_1(Min, Max, Letter, [_|T]) :- is_valid_1(Min, Max, Letter, T).
is_valid_1(0, _, _, []).
is_valid_1((Min, Max, Letter, Password)) :-
is_valid_1(Min, Max, Letter, Password).
is_valid_2((Idx1, Idx2, Letter, Password)) :-
nth1(Idx1, Password, Letter),
not(nth1(Idx2, Password, Letter)).
is_valid_2((Idx1, Idx2, Letter, Password)) :-
not(nth1(Idx1, Password, Letter)),
nth1(Idx2, Password, Letter).
count_valid(P, L, C) :- include(P, L, Valid), length(Valid, C).
main :-
read_lines(Lines),
maplist(parse_record, Lines, Records),
count_valid(is_valid_1, Records, C1),
count_valid(is_valid_2, Records, C2),
format("~d, ~d~n", [C1, C2]).