1
0
Fork 0
mirror of https://github.com/HenkKalkwater/aoc-2020 synced 2024-05-19 21:12:42 +00:00

Added day 6

This commit is contained in:
Chris Josten 2020-12-06 11:49:20 +01:00
parent 9161a33154
commit 4c20c39d74
Signed by: chris
GPG key ID: 1795A594046530AB
3 changed files with 2204 additions and 1 deletions

2128
in/6.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -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
View 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);
}