day 1
This commit is contained in:
commit
4f3ad6d920
6 changed files with 1067 additions and 0 deletions
1
README.md
Normal file
1
README.md
Normal file
|
@ -0,0 +1 @@
|
||||||
|
To evaluate a certain day, just run `nix eval .#day1`
|
12
day1/default.nix
Normal file
12
day1/default.nix
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
{ lib, utils, ... }:
|
||||||
|
with lib;
|
||||||
|
with builtins;
|
||||||
|
with utils;
|
||||||
|
let
|
||||||
|
numbers = map (l: map toInt (splitWhitespace l)) (readLines ./input);
|
||||||
|
left = sortOn noop (map (flip elemAt 0) numbers);
|
||||||
|
right = sortOn noop (map (flip elemAt 1) numbers);
|
||||||
|
in {
|
||||||
|
part1 = listSum (zipListsWith delta left right);
|
||||||
|
part2 = listSum (map (x: x * (count (y: x == y) right)) left);
|
||||||
|
}
|
1000
day1/input
Normal file
1000
day1/input
Normal file
File diff suppressed because it is too large
Load diff
27
flake.lock
Normal file
27
flake.lock
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1733038015,
|
||||||
|
"narHash": "sha256-kIKqS3093Xz5vuvSLk0x1hqo2pFaGwMjnwr3qrTBkzk=",
|
||||||
|
"owner": "nixos",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "767b0e3398fb899d0c88a9f7aecf30dd1cad3166",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nixos",
|
||||||
|
"ref": "nixos-unstable-small",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
17
flake.nix
Normal file
17
flake.nix
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
description = "Advent of Code 2024, but Nix";
|
||||||
|
|
||||||
|
inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable-small"; };
|
||||||
|
|
||||||
|
outputs = { self, nixpkgs }:
|
||||||
|
let
|
||||||
|
inherit (nixpkgs) lib;
|
||||||
|
utils = import ./utils.nix { inherit lib nixpkgs; };
|
||||||
|
in {
|
||||||
|
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixfmt;
|
||||||
|
} // (builtins.mapAttrs
|
||||||
|
(n: v: import "${./.}/${n}" { inherit lib nixpkgs utils; })
|
||||||
|
(lib.filterAttrs (n: v:
|
||||||
|
v == "directory" && (builtins.match "(day[[:digit:]]+)" n) != null)
|
||||||
|
(builtins.readDir ./.)));
|
||||||
|
}
|
10
utils.nix
Normal file
10
utils.nix
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{ lib, ... }:
|
||||||
|
with lib;
|
||||||
|
with builtins; rec {
|
||||||
|
readLines = f: filter (s: (isString s) && s != "") (split "\n" (readFile f));
|
||||||
|
splitWhitespace = s: filter isString (builtins.split "[[:space:]]+" s);
|
||||||
|
abs = i: if i >= 0 then i else i * -1;
|
||||||
|
delta = x: y: abs (x - y);
|
||||||
|
noop = x: x;
|
||||||
|
listSum = foldl' add 0;
|
||||||
|
}
|
Loading…
Reference in a new issue