-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.d
executable file
·41 lines (32 loc) · 1.03 KB
/
main.d
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
void main()
{
import std.algorithm, std.array, std.conv, std.file, std.range, std.stdio;
import std.string;
immutable data = read("../data.txt")
.to!(char[])
.splitter
.array;
auto p1 = data
.map!(s => s.array.sort.group)
.filter!(a => a.count!(t => (t[1] == 2) || (t[1] == 3)) > 0)
.map!(a => a.filter!(t => (t[1] == 2) || (t[1] == 3)).array)
.array;
auto part1 = p1.count!(a => a.count!(t => t[1] == 2) > 0) *
p1.count!(a => a.count!(t => t[1] == 3) > 0);
import mir.combinatorics;
auto compare = function string(string t, string u) {
auto tmp = t
.zip(u)
.filter!(x => x[0] == x[1])
.map!(x => x[0])
.array
.to!string;
return tmp.length == t.length - 1 ? tmp : null;
};
auto part2 = data.combinations(2)
.map!(x => compare(x[0], x[1]))
.find!(x => x !is null)
.front;
"Part 1: %s".writefln(part1);
"Part 2: %s".writefln(part2);
}