mirror of
https://github.com/HenkKalkwater/aoc-2020
synced 2024-11-22 11:05:18 +00:00
Add some support for bigboy versions of exercises
This commit is contained in:
parent
4c20c39d74
commit
4f17dbae8e
|
@ -9,3 +9,6 @@ HOW TO RUN:
|
|||
./oac-2020 [DAY] [PART] <INPUT_FILE> <DAY_SPECIFIC_ARGUMENTS...>
|
||||
|
||||
The program reads it input from stdin if INPUT_FILE is not specified or if INPUT_FILE is equal to "-".
|
||||
|
||||
BIG BOYS:
|
||||
https://ls-a.xyz/advent/2020/bigboy.html
|
||||
|
|
100000
in/bigboy/1.txt
Normal file
100000
in/bigboy/1.txt
Normal file
File diff suppressed because it is too large
Load diff
500000
in/bigboy/2.txt
Normal file
500000
in/bigboy/2.txt
Normal file
File diff suppressed because it is too large
Load diff
10000
in/bigboy/3.txt
Normal file
10000
in/bigboy/3.txt
Normal file
File diff suppressed because it is too large
Load diff
1000196
in/bigboy/4.txt
Normal file
1000196
in/bigboy/4.txt
Normal file
File diff suppressed because it is too large
Load diff
|
@ -17,7 +17,7 @@ import dayutil;
|
|||
|
||||
immutable string progName = "aoc-2020";
|
||||
|
||||
Variant function(int, File, string[])[] programs = [
|
||||
Variant function(int, File, bool, string[])[] programs = [
|
||||
&day1.run,
|
||||
&day2.run,
|
||||
&day3.run,
|
||||
|
@ -42,7 +42,10 @@ void printUsage(string name, string message) {
|
|||
}
|
||||
|
||||
void main(string[] args) {
|
||||
bool bigboy = false;
|
||||
int day;
|
||||
getopt(args,
|
||||
"bigboy", "Execute the so-named 'biboy' variant of this exercise", &bigboy);
|
||||
if (args.length < 3) {
|
||||
printUsage(args[0]);
|
||||
}
|
||||
|
@ -75,7 +78,7 @@ void main(string[] args) {
|
|||
}
|
||||
|
||||
try {
|
||||
Variant result = programs[day - 1](part, file, args[3..$]);
|
||||
Variant result = programs[day - 1](part, file, bigboy, args[3..$]);
|
||||
writeln(result);
|
||||
} catch(ArgumentException e) {
|
||||
printUsage(args[0], e.msg);
|
||||
|
|
|
@ -12,24 +12,27 @@ import dayutil;
|
|||
|
||||
immutable string progName = "aoc-2020";
|
||||
|
||||
Variant run(int part, File input, string[] args) {
|
||||
Variant run(int part, File input, bool bigboy, string[] args) {
|
||||
|
||||
/* For each line on stdin, copy it, map it to an integer and sort it.
|
||||
Sorting a range makes it a SortedRange and functions like contains(range, elem)
|
||||
will make use of optimised implementations, in the case of contains(range, elem)
|
||||
it will use a binary search instead of a linear search */
|
||||
auto numbers = input.byLineCopy.map!(a => to!int(a)).array.sort;
|
||||
int target = bigboy ? 99920044 : 2020;
|
||||
|
||||
Variant solution = parts!int(part, partial!(part1, numbers), partial!(part2, numbers));
|
||||
Variant solution = parts!int(part,
|
||||
() => part1(numbers, target),
|
||||
() => part2(numbers, target));
|
||||
enforce(solution >= 0, "No solutions found");
|
||||
return solution;
|
||||
}
|
||||
|
||||
int part1(SortedRange!(int[]) numbers) {
|
||||
int part1(SortedRange!(int[]) numbers, int target) {
|
||||
int result = -1;
|
||||
|
||||
foreach (ref int a; numbers) {
|
||||
int b = 2020 - a;
|
||||
int b = target - a;
|
||||
if (numbers.contains(b)) {
|
||||
result = b * a;
|
||||
break;
|
||||
|
@ -43,11 +46,11 @@ unittest {
|
|||
assert(part1(numbers) == 514579);
|
||||
}
|
||||
|
||||
int part2(SortedRange!(int[]) numbers) {
|
||||
int part2(SortedRange!(int[]) numbers, int target) {
|
||||
int result = -1;
|
||||
foreach (ref int a; numbers) {
|
||||
foreach (ref int b; numbers) {
|
||||
int c = 2020 - b - a;
|
||||
int c = target - b - a;
|
||||
if (numbers.contains(c)) {
|
||||
result = c * b * a;
|
||||
goto exit;
|
||||
|
|
|
@ -6,7 +6,7 @@ import std.variant;
|
|||
|
||||
import dayutil;
|
||||
|
||||
Variant run(int day, File input, string[] args) {
|
||||
Variant run(int day, File input, bool bigboy, string[] args) {
|
||||
auto lines = input.byLine;
|
||||
Variant count = parts!ulong(day,
|
||||
() => lines.count!(l => isPasswordValid1(l)),
|
||||
|
|
|
@ -9,7 +9,7 @@ import std.variant;
|
|||
|
||||
import dayutil;
|
||||
|
||||
Variant run(int part, File input, string[] args) {
|
||||
Variant run(int part, File input, bool bigboy, string[] args) {
|
||||
auto inputData = stdin.byLineCopy.array;
|
||||
Variant count = parts!size_t(part,
|
||||
() => inputData.countTrees1,
|
||||
|
|
|
@ -47,7 +47,7 @@ struct Passport {
|
|||
&& eyeColour.length > 0 && passwordId.length > 0;
|
||||
}
|
||||
}
|
||||
Variant run(int part, File input, string[] args) {
|
||||
Variant run(int part, File input, bool bigboy, string[] args) {
|
||||
auto lines = input.byLineCopy.array;
|
||||
Variant result = parts!size_t(part,
|
||||
() => part1(lines),
|
||||
|
|
|
@ -8,7 +8,7 @@ import std.variant;
|
|||
|
||||
import dayutil;
|
||||
|
||||
Variant run(int part, File file, string[] args) {
|
||||
Variant run(int part, File file, bool bigboy, string[] args) {
|
||||
auto lines = file.byLine;
|
||||
auto seats = lines.map!(x => determineSeat(to!string(x.array)));
|
||||
Variant result = parts!int(part,
|
||||
|
|
|
@ -8,7 +8,7 @@ import std.variant;
|
|||
|
||||
import dayutil;
|
||||
|
||||
Variant run(int part, File file, string[] args) {
|
||||
Variant run(int part, File file, bool bigboy, string[] args) {
|
||||
auto lines = file.byLineCopy.array;
|
||||
Variant result = parts!size_t(part,
|
||||
() => part1(lines),
|
||||
|
|
Loading…
Reference in a new issue