mirror of
https://github.com/RGBCube/AdventOfCode
synced 2025-07-29 04:57:43 +00:00
Add day 1 part 1
This commit is contained in:
parent
4ecd58f31e
commit
cd153bf92a
3 changed files with 133 additions and 0 deletions
32
2023/2-1.nix
Normal file
32
2023/2-1.nix
Normal file
|
@ -0,0 +1,32 @@
|
|||
{ lib, input }:
|
||||
|
||||
with builtins; with lib; rec {
|
||||
lines = splitString "\n" input;
|
||||
|
||||
lineToId = line: removePrefix "Game " (head (splitString ": " line));
|
||||
lineToRawRGBListList = line:
|
||||
map (splitString ", ")
|
||||
(splitString "; "
|
||||
(last (splitString ": " line)));
|
||||
|
||||
# [ "1 blue" "2 red" ] => { blue = 1; red = 2; }
|
||||
parseRawRGBList = rawList:
|
||||
listToAttrs (map (entry: let
|
||||
parts = splitString " " entry;
|
||||
in {
|
||||
name = last parts;
|
||||
value = head parts;
|
||||
}) rawList);
|
||||
|
||||
# [ { fst = "1"; snd = [ { blue = 1; red = 2; green = 123; } ]; } ]
|
||||
games = zipLists (map lineToId lines) (map (map parseRawRGBList) (map lineToRawRGBListList lines));
|
||||
|
||||
rgbPossible = rgb:
|
||||
(fromJSON (rgb.red or "0")) <= 12 &&
|
||||
(fromJSON (rgb.green or "0")) <= 13 &&
|
||||
(fromJSON (rgb.blue or "0")) <= 14;
|
||||
|
||||
possibleGamesLists = filter (game: all rgbPossible game.snd) games;
|
||||
|
||||
result = foldl' add 0 (map (game: fromJSON game.fst) possibleGamesLists);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue