day 2
This commit is contained in:
parent
bee760c3e6
commit
bb768f9ae5
3 changed files with 1016 additions and 1 deletions
13
day2/default.nix
Normal file
13
day2/default.nix
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
{ lib, utils, ... }:
|
||||||
|
with lib;
|
||||||
|
with builtins;
|
||||||
|
with utils;
|
||||||
|
let
|
||||||
|
records = map (l: map toInt (splitWhitespace l)) (readLines ./input);
|
||||||
|
getMaxDelta = l: (foldl' (prev: value: { inherit value; maxDelta = max prev.maxDelta (delta prev.value value); } ) { value = head l; maxDelta = 0; } l).maxDelta;
|
||||||
|
isSafe = r: let sorted = sort lessThan r; in (r == sorted || r == (reverseList sorted)) && allUnique r && (3 >= getMaxDelta r);
|
||||||
|
isSafeDampened = r: any isSafe ([ r ] ++ (map (deleteAt r) (range 0 (length r))));
|
||||||
|
in {
|
||||||
|
part1 = count isSafe records;
|
||||||
|
part2 = count isSafeDampened records;
|
||||||
|
}
|
1000
day2/input
Normal file
1000
day2/input
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,10 +1,12 @@
|
||||||
{ lib, ... }:
|
{ lib, ... }:
|
||||||
with lib;
|
with lib;
|
||||||
with builtins; rec {
|
with builtins; rec {
|
||||||
readLines = f: let lines = filter isString (split "\n" (readFile f)); in take ((length lines) - 1) lines;
|
readLines = f: init (filter isString (split "\n" (readFile f)));
|
||||||
splitWhitespace = s: filter isString (builtins.split "[[:space:]]+" s);
|
splitWhitespace = s: filter isString (builtins.split "[[:space:]]+" s);
|
||||||
abs = i: if i >= 0 then i else i * -1;
|
abs = i: if i >= 0 then i else i * -1;
|
||||||
delta = x: y: abs (x - y);
|
delta = x: y: abs (x - y);
|
||||||
listSum = foldl' add 0;
|
listSum = foldl' add 0;
|
||||||
|
deleteAt = l: i: (take i l) ++ (drop (i+1) l);
|
||||||
eq = x: y: x == y;
|
eq = x: y: x == y;
|
||||||
|
greaterThan = x: y: x > y;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue