-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3_part2.nix
50 lines (50 loc) · 857 Bytes
/
day3_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
let
inherit (builtins)
filter
foldl'
match
;
inherit (import <nixpkgs/lib>)
toInt
;
inherit (import ./mylib.nix)
_0
_1
map_rec
match_all_with_lenrange
ne
split_
sum
unreachable
;
filter_donts = list:
(foldl'
(acc: el:
acc // (
if el == "do" then
{ do = true; }
else if el == "don't" then
{ do = false; }
else if acc.do then
if match "[0-9]{1,3},[0-9]{1,3}" el != null then
{ list = acc.list ++ [el]; }
else
unreachable ""
else
{}
)
)
{ do = true; list = []; }
list
).list
;
in
input:
input
|> match_all_with_lenrange 4 12 ''mul\(([0-9]{1,3},[0-9]{1,3})\)|(do)\(\)|(don't)\(\)''
|> filter (ne null) # where these nulls come from??
|> filter_donts
|> map (split_ ",")
|> map_rec toInt
|> map (xy: (_0 xy) * (_1 xy))
|> sum