Init
This commit is contained in:
commit
a3766a0666
13
1/solution.nix
Normal file
13
1/solution.nix
Normal file
@ -0,0 +1,13 @@
|
||||
with import ../utilities.nix;
|
||||
|
||||
let
|
||||
input = readFile ./input;
|
||||
elfCalories = map (grp: map toInt (splitString "\n" grp)) (splitString "\n\n" input);
|
||||
totalsPerElf = map sum elfCalories;
|
||||
|
||||
part1 = maxList totalsPerElf;
|
||||
part2 = sum (topOfList (sortList totalsPerElf) 3);
|
||||
in {
|
||||
inherit part1;
|
||||
inherit part2;
|
||||
}
|
35
2/solution.nix
Normal file
35
2/solution.nix
Normal file
@ -0,0 +1,35 @@
|
||||
with import ../utilities.nix;
|
||||
|
||||
let
|
||||
# read file
|
||||
input = readFile ./input;
|
||||
matches = map (line: convertMvId (splitString " " line)) (splitString "\n" input);
|
||||
|
||||
# convert moves to numbers for opponent and player
|
||||
# Rock -> 0, Paper -> 1, Scissors 2
|
||||
mvId = c: m: (charToInt m) - (charToInt c);
|
||||
convertMvId = pair: split pair (a: b: [(mvId "A" a) (mvId "X" b)] );
|
||||
|
||||
# scoring matches
|
||||
isWin = a: b: (mod (a+1) 3) == b;
|
||||
isTie = a: b: a == b;
|
||||
getScore = a: b:
|
||||
b + 1 + # add score for the move itself
|
||||
(if isWin a b then 6
|
||||
else if isTie a b then 3
|
||||
else 0); # loss
|
||||
totalScore = m: sum (map (pair: split pair getScore) m);
|
||||
|
||||
# part 2
|
||||
mkDraw = a: a;
|
||||
mkWin = a: (mod (a+1) 3);
|
||||
mkLoss = a: (mod (a+2) 3);
|
||||
bestMove = a: b:
|
||||
if b == 0 then mkLoss a
|
||||
else if b == 1 then mkDraw a
|
||||
else mkWin a;
|
||||
makeBestMoves = m: map (pair: split pair (a: b: [a (bestMove a b)] )) m;
|
||||
in {
|
||||
part1 = totalScore matches;
|
||||
part2 = totalScore (makeBestMoves matches);
|
||||
}
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 zuckerberg
|
||||
|
||||
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.
|
7
README.md
Normal file
7
README.md
Normal file
@ -0,0 +1,7 @@
|
||||
# AoC 2022 solved using Nix
|
||||
|
||||
To evaluate, go to the directory of a day and run:
|
||||
|
||||
```sh
|
||||
nix eval -f solution.nix
|
||||
```
|
19
utilities.nix
Normal file
19
utilities.nix
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
let
|
||||
lib = import <nixpkgs/lib>;
|
||||
utils = rec {
|
||||
# Passthrough trace for debugging
|
||||
pTrace = v: lib.traceSeq v v;
|
||||
# find the total sum of a int list
|
||||
sum = l: lib.foldr (x: y: x + y) 0 l;
|
||||
# splits a list of length two into two params then they're passed to a func
|
||||
split = pair: f: f (lib.head pair) (lib.last pair);
|
||||
# Finds the max value in a list
|
||||
maxList = l: lib.foldr lib.max 0 l;
|
||||
# Sorts a int list. Greatest value first
|
||||
sortList = l: lib.sort (x: y: x > y) l;
|
||||
# Returns the top cnt items of the list
|
||||
topOfList = l: cnt: lib.take cnt l;
|
||||
};
|
||||
in
|
||||
lib // lib.strings // builtins // utils
|
Loading…
x
Reference in New Issue
Block a user