1
0
Fork 0
mirror of https://github.com/HenkKalkwater/aoc-2020 synced 2024-11-22 11:05:18 +00:00
aoc-2020/source/day6.d

74 lines
969 B
D

import std.algorithm;
import std.array;
import std.range;
import std.string;
import std.stdio;
import std.traits;
import std.variant;
import dayutil;
Variant run(int part, File file, bool bigboy, string[] args) {
auto lines = file.byLineCopy.array;
Variant result = parts!size_t(part,
() => part1(lines),
() => part2(lines));
return result;
}
size_t part1(Range)(Range range) if (isInputRange!Range){
return range.splitter!(x => x.length == 0)
.map!(x => x.joiner.array.sort.uniq.count).sum;
}
unittest {
string input = q"EOS
abc
a
b
c
ab
ac
a
a
a
a
b
EOS";
assert(input.lineSplitter.part1() == 11);
}
size_t part2(Range)(Range range) if (isInputRange!Range){
return range.splitter!(x => x.length == 0)
.map!((x) {
size_t people = x.length;
return x.joiner.array.sort.group.filter!(x => x[1] == people).count;
}).sum;
}
unittest {
string input = q"EOS
abc
a
b
c
ab
ac
a
a
a
a
b
EOS";
assert(input.lineSplitter.part2() == 6);
}