Skip to content

Commit

Permalink
[Nix] Remove dependency on nixpkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
Defelo committed Sep 20, 2024
1 parent b3e58e2 commit b997aaa
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 35 deletions.
11 changes: 7 additions & 4 deletions Nix/2015/01.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
let
inherit (import ../lib) lib input solution scan;
inherit (lib) pipe trim stringToCharacters flip getAttr last;
inherit (lib.lists) findFirstIndex;
lib = import ../lib;
inherit (builtins) getAttr add;
inherit (lib.aoc) input solution;
inherit (lib.functions) pipe flip;
inherit (lib.lists) scan last findFirstIndex;
inherit (lib.strings) stringToCharacters trim;

chars = {
"(" = 1;
Expand All @@ -11,7 +14,7 @@ let
trim
stringToCharacters
(map (flip getAttr chars))
(scan (acc: x: acc + x) 0)
(scan add 0)
];
in
solution {
Expand Down
32 changes: 19 additions & 13 deletions Nix/2023/01.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
let
inherit (import ../lib) lib input solution;
inherit (builtins) fromJSON head filter isList split listToAttrs genList elemAt length stringLength substring attrNames foldl' add;
inherit (lib) splitString trim flatten hasPrefix last;
lib = import ../lib;
inherit (builtins) fromJSON head filter isList split listToAttrs length genList elemAt stringLength substring attrNames foldl' add;
inherit (lib.aoc) input solution;
inherit (lib.lists) flatten last;
inherit (lib.strings) splitLines trim hasPrefix;

lines = splitLines (trim input);

lines = splitString "\n" (trim input);
digits = line: map (s: fromJSON (head s)) (filter isList (split "([0-9])" line));
nums = let
words = ["one" "two" "three" "four" "five" "six" "seven" "eight" "nine"];
wordList = genList (i: {
name = elemAt words i;
value = i + 1;
}) (length words);
digitList =
genList (i: {
name = toString i;
value = i;
})
10;
in
listToAttrs (genList (i: {
name = elemAt words i;
value = i + 1;
}) (length words)
++ (genList (i: {
name = toString i;
value = i;
})
10));
listToAttrs (wordList ++ digitList);

prefixes = s: let len = stringLength s; in genList (i: substring i len s) len;
findWords = s: flatten (map (p: filter (num: hasPrefix num p) (attrNames nums)) (prefixes s));
findNums = s: map (num: nums.${num}) (findWords s);
Expand Down
8 changes: 8 additions & 0 deletions Nix/lib/aoc.nix
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");};
}
37 changes: 19 additions & 18 deletions Nix/lib/default.nix
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
6 changes: 6 additions & 0 deletions Nix/lib/functions.nix
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);
}
38 changes: 38 additions & 0 deletions Nix/lib/lists.nix
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];
}
26 changes: 26 additions & 0 deletions Nix/lib/strings.nix
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;
}

0 comments on commit b997aaa

Please sign in to comment.