mirror of
https://github.com/HenkKalkwater/aoc-2020
synced 2024-11-22 02:55:18 +00:00
Added day 6
This commit is contained in:
parent
9161a33154
commit
4c20c39d74
|
@ -12,6 +12,7 @@ import day2;
|
|||
import day3;
|
||||
import day4;
|
||||
import day5;
|
||||
import day6;
|
||||
import dayutil;
|
||||
|
||||
immutable string progName = "aoc-2020";
|
||||
|
@ -22,6 +23,7 @@ Variant function(int, File, string[])[] programs = [
|
|||
&day3.run,
|
||||
&day4.run,
|
||||
&day5.run,
|
||||
&day6.run,
|
||||
];
|
||||
|
||||
void printUsage(string name) {
|
||||
|
@ -51,7 +53,7 @@ void main(string[] args) {
|
|||
}
|
||||
|
||||
if (day <= 0 || day > programs.length) {
|
||||
printUsage(args[0], "[day] must be between 1 and %d".format(programs.length - 1));
|
||||
printUsage(args[0], "[day] must be between 1 and %d".format(programs.length));
|
||||
}
|
||||
|
||||
int part;
|
||||
|
|
73
source/day6.d
Normal file
73
source/day6.d
Normal file
|
@ -0,0 +1,73 @@
|
|||
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, 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);
|
||||
}
|
Loading…
Reference in a new issue