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;
|
with utils;
|
||||||
let
|
let
|
||||||
records = map (l: map toInt (splitWhitespace l)) (readLines ./input);
|
records = map (l: map toInt (splitWhitespace l)) (readLines ./input);
|
||||||
getMaxDelta = l:
|
getDeltas = r: zipListsWith sub (init r) (drop 1 r);
|
||||||
(foldl' (prev: value: {
|
|
||||||
inherit value;
|
|
||||||
maxDelta = max prev.maxDelta (delta prev.value value);
|
|
||||||
}) {
|
|
||||||
value = head l;
|
|
||||||
maxDelta = 0;
|
|
||||||
} l).maxDelta;
|
|
||||||
isSafe = r:
|
isSafe = r:
|
||||||
let sorted = sort lessThan r;
|
let deltas = getDeltas r;
|
||||||
in (r == sorted || r == (reverseList sorted)) && allUnique r
|
in (all (d: -3 <= d && d < 0) deltas) || (all (d: 3 >= d && d > 0) deltas);
|
||||||
&& (3 >= getMaxDelta r);
|
isSafeDampened = r: any isSafe (map (deleteAt r) (listRange r));
|
||||||
isSafeDampened = r:
|
|
||||||
any isSafe ([ r ] ++ (map (deleteAt r) (range 0 (length r))));
|
|
||||||
in {
|
in {
|
||||||
part1 = count isSafe records;
|
part1 = count isSafe records;
|
||||||
part2 = count isSafeDampened records;
|
part2 = count isSafeDampened records;
|
||||||
|
|
|
@ -9,4 +9,5 @@ with builtins; rec {
|
||||||
deleteAt = l: i: (take i l) ++ (drop (i + 1) l);
|
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;
|
greaterThan = x: y: x > y;
|
||||||
|
listRange = l: range 0 (length l);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue