advent-of-code/day06/default.nix

33 lines
584 B
Nix
Raw Normal View History

2022-12-06 08:34:47 +01:00
{ pkgs, lib }:
with lib;
let
2022-12-06 11:12:17 +01:00
countWithNUntil = n: pred: list: let
inner = list': count:
if pred (take n list')
2022-12-06 08:34:47 +01:00
then count
2022-12-06 11:12:17 +01:00
else inner (tail list') (count + 1);
in inner list 0;
2022-12-06 08:34:47 +01:00
2022-12-06 09:47:16 +01:00
allItemsAreUnique = l: l == []
2022-12-06 11:12:17 +01:00
|| !(elem (head l) (tail l)) && allItemsAreUnique (tail l);
2022-12-06 08:34:47 +01:00
2022-12-06 11:12:17 +01:00
answerN = n: pipe ./input.txt [
fileContents
stringToCharacters
(countWithNUntil n 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}
''