-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2_part2.nix
80 lines (74 loc) · 1.52 KB
/
day2_part2.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
let
pkgs = import <nixpkgs> { };
inherit (pkgs) lib;
in
with builtins; let
input =
lib.strings.splitString
"\n"
(lib.strings.removeSuffix
"\n"
(readFile ./day_2_input.txt));
gameParser = line:
let
gameNum =
lib.strings.toInt
(elemAt
(match ''Game ([0-9]+):.*'' line)
0);
setsRaw =
lib.strings.removePrefix
"Game ${gameNum}: "
line;
sets =
lib.strings.splitString ";" line;
in
{
id = gameNum;
sets = sets;
};
games =
lib.lists.forEach
input
gameParser;
getCount = set: color:
lib.strings.toInt
(elemAt
(
let res =
(match
".*? ([0-9]+) ${color}.*"
set); in if res == null then [ "0" ] else res
)
0);
setsHandler = { id, sets }:
let
red =
lib.lists.forEach
sets
(set: getCount set "red");
blue =
lib.lists.forEach
sets
(set: getCount set "blue");
green =
lib.lists.forEach
sets
(set: getCount set "green");
in
{
inherit id;
r = elemAt (lib.lists.sort (x: y: x > y) red) 0;
b = elemAt (lib.lists.sort (x: y: x > y) blue) 0;
g = elemAt (lib.lists.sort (x: y: x > y) green) 0;
};
gameResults = lib.lists.forEach
games
setsHandler;
result =
foldl'
(acc: x: acc + (with x; r * g * b))
0
gameResults;
in
result