Impl d1p1 in nix
This commit is contained in:
parent
e9d8b35d8c
commit
3400e9db04
2 changed files with 44 additions and 3 deletions
41
day1/part1.nix
Normal file
41
day1/part1.nix
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
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": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1733015953,
|
"lastModified": 1733759999,
|
||||||
"narHash": "sha256-t4BBVpwG9B4hLgc6GUBuj3cjU7lP/PJfpTHuSqE+crk=",
|
"narHash": "sha256-463SNPWmz46iLzJKRzO3Q2b0Aurff3U1n0nYItxq7jU=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "ac35b104800bff9028425fec3b6e8a41de2bbfff",
|
"rev": "a73246e2eef4c6ed172979932bc80e1404ba2d56",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
|
Loading…
Reference in a new issue