This repository has been archived by the owner on Sep 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.nix
47 lines (47 loc) · 1.61 KB
/
2.nix
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
let
maxInt = 9223372036854775807;
countChar = string: char:
if builtins.stringLength string == 0 then
0
else if builtins.substring 0 1 string == char then
1 + (countChar (builtins.substring 1 maxInt string) char)
else
countChar (builtins.substring 1 maxInt string) char;
parseRecord = record:
let
fields = builtins.match "([[:digit:]]+)-([[:digit:]]+) ([[:alpha:]]): ([[:alpha:]]+)" record;
in
if fields == null then
null
else {
start = builtins.fromJSON (builtins.elemAt fields 0);
end = builtins.fromJSON (builtins.elemAt fields 1);
char = builtins.elemAt fields 2;
pass = builtins.elemAt fields 3;
};
isValid = record:
let
numChars = countChar record.pass record.char;
in
numChars >= record.start && numChars <= record.end;
hasCharAt = string: char: position:
if builtins.stringLength string < position then
false
else
builtins.substring (position - 1) 1 string == char;
isValid2 = record:
let
first = hasCharAt record.pass record.char record.start;
second = hasCharAt record.pass record.char record.end;
in
(!first && second) || (first && !second);
toLines = string:
builtins.filter builtins.isString (builtins.split "\n" string);
countValid = list: validator:
builtins.length (builtins.filter (x: x) (builtins.map validator list));
input = builtins.readFile ./2.in;
records = builtins.filter (x: !builtins.isNull x) (builtins.map parseRecord (toLines input));
in {
"1" = countValid records isValid;
"2" = countValid records isValid2;
}