advent-of-code/day06/default.nix

54 lines
1.1 KiB
Nix
Raw Normal View History

2022-12-06 08:34:47 +01:00
{ pkgs, lib }:
with lib;
let
input = stringToCharacters (fileContents ./input.txt);
foldl'' = f: l: let
initial = take 2 l;
2022-12-06 09:47:16 +01:00
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)));
2022-12-06 08:34:47 +01:00
countUntil = pred: l: let
innerCount = list: count:
if pred (head list)
then count
2022-12-06 09:47:16 +01:00
else innerCount (tail list) (count + 1);
2022-12-06 08:34:47 +01:00
in innerCount l 0;
2022-12-06 09:47:16 +01:00
allItemsAreUnique = l: l == []
|| (!(elem (head l) (tail l))
&& allItemsAreUnique (tail l));
2022-12-06 08:34:47 +01:00
2022-12-06 09:47:16 +01:00
answerN = n: pipe input [
(zipNSelfDrop1 n)
2022-12-06 08:34:47 +01:00
(map attrValues)
(countUntil allItemsAreUnique)
2022-12-06 09:47:16 +01:00
(add n)
2022-12-06 08:34:47 +01:00
toString
];
2022-12-06 09:47:16 +01:00
answer1 = answerN 4;
answer2 = answerN 14;
2022-12-06 08:34:47 +01:00
in pkgs.writeText "answers" ''
Task1:
${answer1}
Task2:
${answer2}
''