Day 01
This commit is contained in:
commit
d5e9ce2d39
|
@ -0,0 +1 @@
|
||||||
|
result
|
|
@ -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.
|
|
@ -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
|
|
@ -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}
|
||||||
|
''
|
File diff suppressed because it is too large
Load Diff
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue