mirror of
https://github.com/RGBCube/AdventOfCode
synced 2025-07-26 19:47:46 +00:00
Add day 1 part 2
This commit is contained in:
parent
3a223b8219
commit
dfda2c002c
4 changed files with 1047 additions and 4 deletions
|
@ -1,6 +1,6 @@
|
||||||
{ lib, input }:
|
{ lib, input }:
|
||||||
|
|
||||||
with lib; with builtins; let
|
with builtins; with lib; rec {
|
||||||
lines = splitString "\n" input;
|
lines = splitString "\n" input;
|
||||||
|
|
||||||
linesChars = map (splitString "") lines;
|
linesChars = map (splitString "") lines;
|
||||||
|
@ -14,4 +14,4 @@ with lib; with builtins; let
|
||||||
sumOfLinesNumbers = foldl' add 0 (map fromJSON linesNumbers);
|
sumOfLinesNumbers = foldl' add 0 (map fromJSON linesNumbers);
|
||||||
|
|
||||||
result = sumOfLinesNumbers;
|
result = sumOfLinesNumbers;
|
||||||
in result
|
}
|
||||||
|
|
1000
2023/1-2.in
Normal file
1000
2023/1-2.in
Normal file
File diff suppressed because it is too large
Load diff
42
2023/1-2.nix
Normal file
42
2023/1-2.nix
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
{ lib, input }:
|
||||||
|
|
||||||
|
with builtins; with lib; rec {
|
||||||
|
digits = {
|
||||||
|
zero = "0";
|
||||||
|
one = "1";
|
||||||
|
two = "2";
|
||||||
|
three = "3";
|
||||||
|
four = "4";
|
||||||
|
five = "5";
|
||||||
|
six = "6";
|
||||||
|
seven = "7";
|
||||||
|
eight = "8";
|
||||||
|
nine = "9";
|
||||||
|
};
|
||||||
|
|
||||||
|
toDigit = maybeDigit: if digits ? "${maybeDigit}" then
|
||||||
|
digits.${maybeDigit}
|
||||||
|
else
|
||||||
|
maybeDigit;
|
||||||
|
|
||||||
|
allNeedles = flatten (mapAttrsToList (key: value: [ key (toString value) ]) digits);
|
||||||
|
allNeedlesReverse = map reverseString allNeedles;
|
||||||
|
|
||||||
|
reverseString = string: concatStrings (reverseList (stringToCharacters string));
|
||||||
|
|
||||||
|
firstMatch = haystack: needles: let
|
||||||
|
prefixNeedle = findFirst (needle: hasPrefix needle haystack) null needles;
|
||||||
|
in if prefixNeedle != null then
|
||||||
|
prefixNeedle
|
||||||
|
else
|
||||||
|
firstMatch (substring 1 (stringLength haystack - 1) haystack) needles;
|
||||||
|
|
||||||
|
firstLast = line: let
|
||||||
|
first = firstMatch line allNeedles;
|
||||||
|
last = reverseString (firstMatch (reverseString line) allNeedlesReverse);
|
||||||
|
in fromJSON (toDigit first + toDigit last);
|
||||||
|
|
||||||
|
lines = splitString "\n" input;
|
||||||
|
|
||||||
|
result = foldl' add 0 (map firstLast lines);
|
||||||
|
}
|
|
@ -10,14 +10,15 @@
|
||||||
outputs = { nixpkgs, ... }: let
|
outputs = { nixpkgs, ... }: let
|
||||||
lib = nixpkgs.lib;
|
lib = nixpkgs.lib;
|
||||||
|
|
||||||
pathToResult = path: import ./${path}.nix {
|
pathToResult = path: (import ./${path}.nix {
|
||||||
inherit lib;
|
inherit lib;
|
||||||
|
|
||||||
input = builtins.concatStringsSep "\n"
|
input = builtins.concatStringsSep "\n"
|
||||||
(builtins.filter (line: line != "")
|
(builtins.filter (line: line != "")
|
||||||
(lib.splitString "\n" (builtins.readFile ./${path}.in)));
|
(lib.splitString "\n" (builtins.readFile ./${path}.in)));
|
||||||
};
|
}).result;
|
||||||
in lib.genAttrs [
|
in lib.genAttrs [
|
||||||
"2023/1-1"
|
"2023/1-1"
|
||||||
|
"2023/1-2"
|
||||||
] pathToResult;
|
] pathToResult;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue