alternative day 2 solution: zipListsWith for deltas, just two checks
This commit is contained in:
parent
315d8a4aa3
commit
ff7f326e1d
2 changed files with 5 additions and 13 deletions
|
@ -4,20 +4,11 @@ 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;
|
||||
getDeltas = r: zipListsWith sub (init r) (drop 1 r);
|
||||
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))));
|
||||
let deltas = getDeltas r;
|
||||
in (all (d: -3 <= d && d < 0) deltas) || (all (d: 3 >= d && d > 0) deltas);
|
||||
isSafeDampened = r: any isSafe (map (deleteAt r) (listRange r));
|
||||
in {
|
||||
part1 = count isSafe records;
|
||||
part2 = count isSafeDampened records;
|
||||
|
|
|
@ -9,4 +9,5 @@ with builtins; rec {
|
|||
deleteAt = l: i: (take i l) ++ (drop (i + 1) l);
|
||||
eq = x: y: x == y;
|
||||
greaterThan = x: y: x > y;
|
||||
listRange = l: range 0 (length l);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue