Add 2023, Day 01

This commit is contained in:
Oystein Kristoffer Tveit 2023-12-01 21:14:22 +01:00
parent 77faa66e9d
commit 0a837c82bf
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
4 changed files with 1110 additions and 0 deletions

72
2023/day01/default.nix Normal file
View File

@ -0,0 +1,72 @@
{ pkgs, lib }:
with lib;
let
input = pipe (fileContents ./input.txt) [
(splitString "\n")
];
answer1 = pipe input [
(map stringToCharacters)
(x: {
forwards = x;
reverse = map reverseList x;
})
(mapAttrs (_: map (findFirst (x: builtins.match "[[:digit:]]" x == [ ]) null)))
({ forwards, reverse }: zipListsWith (a: b: a + b) forwards reverse)
(map toInt)
(foldr (a: b: a + b) 0)
toString
];
letterNumbers = {
"one" = "1";
"two" = "2";
"three" = "3";
"four" = "4";
"five" = "5";
"six" = "6";
"seven" = "7";
"eight" = "8";
"nine" = "9";
};
reverseString = s: pipe s [
stringToCharacters
reverseList
(builtins.concatStringsSep "")
];
# NOTE: builtins.match somehow always matches the last group first.
# I believe it might be an implementation detail.
answer2 = pipe input [
(x: {
last = pipe x [
(map (builtins.match "^.*([[:digit:]]|${builtins.concatStringsSep "|" (builtins.attrNames letterNumbers)}).*$"))
(map head)
];
first = pipe x [
(map reverseString)
(let
rLetterNumbers = map reverseString (builtins.attrNames letterNumbers);
in map (builtins.match "^.*([[:digit:]]|${builtins.concatStringsSep "|" rLetterNumbers}).*$"))
(map head)
(map reverseString)
];
})
(mapAttrs (_: map (x: letterNumbers.${x} or x)))
({ first, last }: zipListsWith (a: b: a + b) first last)
(map toInt)
(foldr (a: b: a + b) 0)
toString
];
in
pkgs.writeText "answers" ''
Task1:
${answer1}
Task2:
${answer2}
''

1000
2023/day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

26
2023/flake.lock Normal file
View File

@ -0,0 +1,26 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1701263465,
"narHash": "sha256-lNXUIlkfyDyp9Ox21hr+wsEf/IBklLvb6bYcyeXbdRc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "50aa30a13c4ab5e7ba282da460a3e3d44e9d0eb3",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-23.11",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

12
2023/flake.nix Normal file
View File

@ -0,0 +1,12 @@
{
inputs.nixpkgs.url = "nixpkgs/nixos-23.11";
outputs = { self, nixpkgs }: let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
packages.${system} = {
day01 = pkgs.callPackage ./day01 { };
};
};
}