Impl d1p1 in nix
This commit is contained in:
parent
e9d8b35d8c
commit
925e0b1a51
2 changed files with 49 additions and 3 deletions
46
day1/part1.nix
Normal file
46
day1/part1.nix
Normal file
|
@ -0,0 +1,46 @@
|
|||
let
|
||||
# Import nixpkgs lib and input file
|
||||
inherit ((builtins.getFlake (builtins.toString ./..)).inputs.nixpkgs) lib;
|
||||
input = builtins.readFile (builtins.toString ./input.txt);
|
||||
|
||||
# Define functions
|
||||
abs = n: if n < 0 then (-n) else n;
|
||||
|
||||
# Do parsing
|
||||
parsed = ( # Define function to sort lines into two lists
|
||||
lib.lists.foldr
|
||||
(cur: acc: {
|
||||
first = acc.first ++ [(lib.strings.toIntBase10 (builtins.elemAt cur 0))];
|
||||
second = acc.second ++ [(lib.strings.toIntBase10 (builtins.elemAt cur 1))];
|
||||
})
|
||||
{ first = []; second = []; }
|
||||
) ( # Call with input split on newlines and 3 spaces; formatted like [ [ 1 2 ] [ 3 4 ] [ 5 6 ] ]
|
||||
lib.lists.forEach
|
||||
(lib.strings.splitString "\n" input)
|
||||
(e: lib.strings.splitString " " e)
|
||||
);
|
||||
sorted = {
|
||||
first = lib.lists.sort (a: b: a < b) parsed.first;
|
||||
second = lib.lists.sort (a: b: a < b) parsed.second;
|
||||
};
|
||||
reorganized = (
|
||||
lib.lists.imap0
|
||||
(i: e: [ e (builtins.elemAt sorted.second i) ])
|
||||
sorted.first
|
||||
);
|
||||
|
||||
output = (
|
||||
(
|
||||
lib.lists.foldr
|
||||
(
|
||||
cur: acc:
|
||||
acc + (
|
||||
abs (
|
||||
(builtins.elemAt cur 0) - (builtins.elemAt cur 1)
|
||||
)
|
||||
)
|
||||
)
|
||||
0
|
||||
) reorganized
|
||||
);
|
||||
in output
|
|
@ -20,11 +20,11 @@
|
|||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1733015953,
|
||||
"narHash": "sha256-t4BBVpwG9B4hLgc6GUBuj3cjU7lP/PJfpTHuSqE+crk=",
|
||||
"lastModified": 1733759999,
|
||||
"narHash": "sha256-463SNPWmz46iLzJKRzO3Q2b0Aurff3U1n0nYItxq7jU=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "ac35b104800bff9028425fec3b6e8a41de2bbfff",
|
||||
"rev": "a73246e2eef4c6ed172979932bc80e1404ba2d56",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
|
Loading…
Reference in a new issue