2024: init

This commit is contained in:
Oystein Kristoffer Tveit 2024-12-02 16:19:17 +01:00
parent 9d6e2dfaf2
commit a405bda0fa
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
8 changed files with 2149 additions and 0 deletions

43
2024/day01/default.nix Normal file
View File

@ -0,0 +1,43 @@
{ pkgs, lib }:
with lib;
let
input = pipe (fileContents ./input.txt) [
(splitString "\n")
(map (split "[[:space:]]+"))
(map (x: {
fst = elemAt x 0;
snd = elemAt x 2;
}))
(x: {
fst = catAttrs "fst" x;
snd = catAttrs "snd" x;
})
];
abs = x: if x < 0 then -x else x;
answer1 = lib.pipe input [
(mapAttrs (_: xs: sort lessThan (map toInt xs)))
({ fst, snd }: zipListsWith (x: y: abs (x - y)) fst snd)
(foldl add 0)
toString
];
similarity = left: right: let
rightCount = foldl (acc: x: acc // { ${x} = (acc.${x} or 0) + 1; }) { } right;
in foldl add 0 (map (x: (toInt x) * rightCount.${x} or 0) left);
answer2 = lib.pipe input [
({ fst, snd }: similarity fst snd)
toString
];
in
pkgs.writeText "answers" ''
Task1:
${answer1}
Task2:
${answer2}
''

1000
2024/day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

40
2024/day02/default.nix Normal file
View File

@ -0,0 +1,40 @@
{ pkgs, lib }:
with lib;
let
input = pipe (fileContents ./input.txt) [
(splitString "\n")
(map (split "[[:space:]]+"))
(map (filter (x: x != [])))
(map (map toInt))
];
# - The levels are either all increasing or all decreasing.
# - Any two adjacent levels differ by at least one and at most three.
safeCondition = xs: let
zipped = zipLists xs (tail xs);
in (all ({ fst, snd }: let x = fst - snd; in 1 <= x && x <= 3) zipped)
|| (all ({ fst, snd }: let x = fst - snd; in -1 >= x && x >= -3) zipped);
answer1 = lib.pipe input [
(filter safeCondition)
length
toString
];
answer2 = lib.pipe input [
(filter (xs: let
variants = genList (i: (take i xs) ++ (drop (i + 1) xs)) (length xs);
in any safeCondition variants))
length
toString
];
in
pkgs.writeText "answers" ''
Task1:
${answer1}
Task2:
${answer2}
''

1000
2024/day02/input.txt Normal file

File diff suppressed because it is too large Load Diff

27
2024/flake.lock generated Normal file
View File

@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1733015953,
"narHash": "sha256-t4BBVpwG9B4hLgc6GUBuj3cjU7lP/PJfpTHuSqE+crk=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "ac35b104800bff9028425fec3b6e8a41de2bbfff",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

15
2024/flake.nix Normal file
View File

@ -0,0 +1,15 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
};
outputs = { self, nixpkgs }: let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
packages.${system} = {
day01 = pkgs.callPackage ./day01 { };
day02 = pkgs.callPackage ./day02 { };
};
};
}

24
2024/template/default.nix Normal file
View File

@ -0,0 +1,24 @@
{ pkgs, lib }:
with lib;
let
input = pipe (fileContents ./input.txt) [
(splitString "\n")
];
answer1 = lib.pipe input [
(_: "TODO")
];
answer2 = lib.pipe input [
(_: "TODO")
];
in
pkgs.writeText "answers" ''
Task1:
${answer1}
Task2:
${answer2}
''

0
2024/template/input.nix Normal file
View File