1
0
Fork 0
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:
Chris Josten 2020-12-07 06:05:35 +01:00
parent 4c20c39d74
commit 4f17dbae8e
Signed by: chris
GPG key ID: 1795A594046530AB
12 changed files with 1610218 additions and 13 deletions

View file

@ -9,3 +9,6 @@ HOW TO RUN:
./oac-2020 [DAY] [PART] <INPUT_FILE> <DAY_SPECIFIC_ARGUMENTS...> ./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 "-". 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

File diff suppressed because it is too large Load diff

500000
in/bigboy/2.txt Normal file

File diff suppressed because it is too large Load diff

10000
in/bigboy/3.txt Normal file

File diff suppressed because it is too large Load diff

1000196
in/bigboy/4.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -17,7 +17,7 @@ import dayutil;
immutable string progName = "aoc-2020"; immutable string progName = "aoc-2020";
Variant function(int, File, string[])[] programs = [ Variant function(int, File, bool, string[])[] programs = [
&day1.run, &day1.run,
&day2.run, &day2.run,
&day3.run, &day3.run,
@ -42,7 +42,10 @@ void printUsage(string name, string message) {
} }
void main(string[] args) { void main(string[] args) {
bool bigboy = false;
int day; int day;
getopt(args,
"bigboy", "Execute the so-named 'biboy' variant of this exercise", &bigboy);
if (args.length < 3) { if (args.length < 3) {
printUsage(args[0]); printUsage(args[0]);
} }
@ -75,7 +78,7 @@ void main(string[] args) {
} }
try { try {
Variant result = programs[day - 1](part, file, args[3..$]); Variant result = programs[day - 1](part, file, bigboy, args[3..$]);
writeln(result); writeln(result);
} catch(ArgumentException e) { } catch(ArgumentException e) {
printUsage(args[0], e.msg); printUsage(args[0], e.msg);

View file

@ -12,24 +12,27 @@ import dayutil;
immutable string progName = "aoc-2020"; 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. /* 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) 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) will make use of optimised implementations, in the case of contains(range, elem)
it will use a binary search instead of a linear search */ it will use a binary search instead of a linear search */
auto numbers = input.byLineCopy.map!(a => to!int(a)).array.sort; 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"); enforce(solution >= 0, "No solutions found");
return solution; return solution;
} }
int part1(SortedRange!(int[]) numbers) { int part1(SortedRange!(int[]) numbers, int target) {
int result = -1; int result = -1;
foreach (ref int a; numbers) { foreach (ref int a; numbers) {
int b = 2020 - a; int b = target - a;
if (numbers.contains(b)) { if (numbers.contains(b)) {
result = b * a; result = b * a;
break; break;
@ -43,11 +46,11 @@ unittest {
assert(part1(numbers) == 514579); assert(part1(numbers) == 514579);
} }
int part2(SortedRange!(int[]) numbers) { int part2(SortedRange!(int[]) numbers, int target) {
int result = -1; int result = -1;
foreach (ref int a; numbers) { foreach (ref int a; numbers) {
foreach (ref int b; numbers) { foreach (ref int b; numbers) {
int c = 2020 - b - a; int c = target - b - a;
if (numbers.contains(c)) { if (numbers.contains(c)) {
result = c * b * a; result = c * b * a;
goto exit; goto exit;

View file

@ -6,7 +6,7 @@ import std.variant;
import dayutil; import dayutil;
Variant run(int day, File input, string[] args) { Variant run(int day, File input, bool bigboy, string[] args) {
auto lines = input.byLine; auto lines = input.byLine;
Variant count = parts!ulong(day, Variant count = parts!ulong(day,
() => lines.count!(l => isPasswordValid1(l)), () => lines.count!(l => isPasswordValid1(l)),

View file

@ -9,7 +9,7 @@ import std.variant;
import dayutil; 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; auto inputData = stdin.byLineCopy.array;
Variant count = parts!size_t(part, Variant count = parts!size_t(part,
() => inputData.countTrees1, () => inputData.countTrees1,

View file

@ -47,7 +47,7 @@ struct Passport {
&& eyeColour.length > 0 && passwordId.length > 0; && 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; auto lines = input.byLineCopy.array;
Variant result = parts!size_t(part, Variant result = parts!size_t(part,
() => part1(lines), () => part1(lines),

View file

@ -8,7 +8,7 @@ import std.variant;
import dayutil; 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 lines = file.byLine;
auto seats = lines.map!(x => determineSeat(to!string(x.array))); auto seats = lines.map!(x => determineSeat(to!string(x.array)));
Variant result = parts!int(part, Variant result = parts!int(part,

View file

@ -8,7 +8,7 @@ import std.variant;
import dayutil; 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; auto lines = file.byLineCopy.array;
Variant result = parts!size_t(part, Variant result = parts!size_t(part,
() => part1(lines), () => part1(lines),