This commit is contained in:
Oystein Kristoffer Tveit 2022-12-01 21:38:11 +01:00
commit d5e9ce2d39
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
7 changed files with 2370 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
result

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 h7x4
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# Advent of code 2022 - Nix
This is [Advent of Code 2022][aoc2022] (or at least as far as I'm able to get before having to deal with my exams...)
They're written in nix this year.
To run the code:
```
nix build github:h7x4/aoc2022#day01
```
[aoc2022]: https://adventofcode.com/2022

32
day01/default.nix Normal file
View File

@ -0,0 +1,32 @@
{ pkgs, lib, ... }:
with lib;
let
elves = pipe (builtins.readFile ./input.txt) [
(splitString "\n\n")
(map (splitString "\n"))
(map (map toInt))
(map (fold add 0))
];
answer1 = toString (fold max (-1) elves);
max3 = abc: x: if x >= abc.a then { a = x; b = abc.a; c = abc.b; } else
if x >= abc.b then { a = abc.a; b = x; c = abc.b; } else
if x >= abc.c then { inherit (abc) a b; c = x; } else
abc;
answer2 = pipe elves [
(foldl max3 { a = -1; b = -1; c = -1; })
(abc: abc.a + abc.b + abc.c)
toString
];
in pkgs.writeText "answers" ''
Task1:
${answer1}
Task2:
${answer2}
''

2265
day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

26
flake.lock Normal file
View File

@ -0,0 +1,26 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1669834992,
"narHash": "sha256-YnhZGHgb4C3Q7DSGisO/stc50jFb9F/MzHeKS4giotg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "596a8e828c5dfa504f91918d0fa4152db3ab5502",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-22.11",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

12
flake.nix Normal file
View File

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