Day 06 - cleanup 2

This commit is contained in:
Oystein Kristoffer Tveit 2022-12-06 11:12:17 +01:00
parent 53acf593e4
commit e07d1cefec
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
1 changed files with 10 additions and 31 deletions

View File

@ -3,41 +3,20 @@
with lib; with lib;
let let
input = stringToCharacters (fileContents ./input.txt); countWithNUntil = n: pred: list: let
inner = list': count:
foldl'' = f: l: let if pred (take n list')
initial = take 2 l;
base = f (elemAt initial 0) (elemAt initial 1);
in foldl f base (drop 2 l);
zipLists = ls: let
minLength = foldl'' min (map length ls);
f = n: pipe ls [
(map (l: elemAt l n))
(imap0 (i: nameValuePair (toString i)))
listToAttrs
];
in genList f minLength;
zipNSelfDrop1 = n: l:
zipLists (map (i: drop i l) (range 0 (n - 1)));
countUntil = pred: l: let
innerCount = list: count:
if pred (head list)
then count then count
else innerCount (tail list) (count + 1); else inner (tail list') (count + 1);
in innerCount l 0; in inner list 0;
allItemsAreUnique = l: l == [] allItemsAreUnique = l: l == []
|| (!(elem (head l) (tail l)) || !(elem (head l) (tail l)) && allItemsAreUnique (tail l);
&& allItemsAreUnique (tail l));
answerN = n: pipe input [ answerN = n: pipe ./input.txt [
(zipNSelfDrop1 n) fileContents
(map attrValues) stringToCharacters
(countUntil allItemsAreUnique) (countWithNUntil n allItemsAreUnique)
(add n) (add n)
toString toString
]; ];