-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
123 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
lib: let | ||
inherit (builtins) readFile getEnv; | ||
inherit (lib.strings) optionalString; | ||
in { | ||
input = readFile (getEnv "INPUT"); | ||
|
||
solution = args: args // {__toString = s: "${toString s.p1}\n" + (optionalString (s ? p2) "${toString s.p2}\n");}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
rec { | ||
inherit (import <nixpkgs> {}) lib; | ||
let | ||
pipe = builtins.foldl' (acc: f: f acc); | ||
|
||
scan = f: init: lst: let | ||
mkAcc = out: acc: {inherit out acc;}; | ||
op = x: y: let | ||
acc' = f x.acc y; | ||
in | ||
mkAcc (x.out ++ [acc']) acc'; | ||
result = builtins.foldl' op (mkAcc [] init) lst; | ||
removeDotNix = s: let | ||
removed = builtins.match "(.*)\\.nix" s; | ||
in | ||
result.out; | ||
if removed != null | ||
then builtins.head removed | ||
else s; | ||
|
||
input = builtins.readFile (builtins.getEnv "INPUT"); | ||
|
||
solution = { | ||
p1, | ||
p2 ? null, | ||
}: | ||
"${toString p1}\n" + (lib.optionalString (p2 != null) "${toString p2}\n"); | ||
} | ||
lib = pipe ./. [ | ||
builtins.readDir | ||
builtins.attrNames | ||
(builtins.filter (name: name != "default.nix")) | ||
(map (name: { | ||
name = removeDotNix name; | ||
value = import ./${name} lib; | ||
})) | ||
builtins.listToAttrs | ||
]; | ||
in | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
lib: let | ||
inherit (builtins) foldl'; | ||
in { | ||
flip = f: a: b: f b a; | ||
pipe = foldl' (acc: f: f acc); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
lib: let | ||
inherit (builtins) elemAt length head tail isList concatMap; | ||
in rec { | ||
last = lst: elemAt lst (length lst - 1); | ||
|
||
findFirstIndex = pred: default: lst: let | ||
find = lst: | ||
if lst == [] | ||
then null | ||
else if pred (head lst) | ||
then 0 | ||
else let | ||
x = find (tail lst); | ||
in | ||
if x == null | ||
then null | ||
else x + 1; | ||
result = find lst; | ||
in | ||
if result == null | ||
then default | ||
else result; | ||
|
||
scan = f: init: lst: let | ||
mkAcc = out: acc: {inherit out acc;}; | ||
op = x: y: let | ||
acc' = f x.acc y; | ||
in | ||
mkAcc (x.out ++ [acc']) acc'; | ||
result = builtins.foldl' op (mkAcc [] init) lst; | ||
in | ||
result.out; | ||
|
||
flatten = lst: | ||
if isList lst | ||
then concatMap flatten lst | ||
else [lst]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
lib: let | ||
inherit (builtins) match head substring genList stringLength replaceStrings split filter isString; | ||
in rec { | ||
optionalString = x: s: | ||
if x | ||
then s | ||
else ""; | ||
|
||
trim = s: let | ||
chars = " \t\r\n"; | ||
regex = "[${chars}]*(.*[^${chars}])[${chars}]*"; | ||
res = match regex s; | ||
in | ||
optionalString (res != null) (head res); | ||
|
||
charAt = s: i: substring i 1 s; | ||
stringToCharacters = s: genList (charAt s) (stringLength s); | ||
|
||
escape = lst: replaceStrings lst (map (c: "\\${c}") lst); | ||
escapeRegex = escape (stringToCharacters "\\[{()^$?*+|."); | ||
|
||
splitString = sep: s: filter isString (split (escapeRegex sep) s); | ||
splitLines = splitString "\n"; | ||
|
||
hasPrefix = p: s: substring 0 (stringLength p) s == p; | ||
} |