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

Add day 9 part 1 and automatically open file for day

This commit is contained in:
Chris Josten 2020-12-09 15:48:02 +01:00
parent 53c5a606ee
commit 3e2632b2af
Signed by: chris
GPG key ID: 1795A594046530AB
3 changed files with 1045 additions and 6 deletions

1000
in/9.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -15,6 +15,7 @@ import day5;
import day6;
import day7;
import day8;
import day9;
import dayutil;
immutable string progName = "aoc-2020";
@ -28,6 +29,7 @@ Variant function(int, File, bool, string[])[] programs = [
&day6.run,
&day7.run,
&day8.run,
&day9.run,
];
void printUsage(string name) {
@ -71,14 +73,20 @@ void main(string[] args) {
}
File file;
if (args.length < 4 || args[3] == "-") {
file = stdin;
} else {
try {
try {
if (args.length < 4) {
if (bigboy) {
file = File("in/bigboy/%d.txt".format(day), "rb");
} else {
file = File("in/%d.txt".format(day), "rb");
}
} else if(args[3] == "-") {
file = stdin;
} else {
file = File(args[3], "rb");
} catch (ErrnoException e) {
printUsage(args[0], "Error %d while opening input file: %s".format(e.errno, e.message));
}
} catch (ErrnoException e) {
printUsage(args[0], "Error %d while opening input file: %s".format(e.errno, e.message));
}
try {

31
source/day9.d Normal file
View file

@ -0,0 +1,31 @@
import std.array;
import std.algorithm;
import std.range;
import std.stdio;
import std.traits;
import std.variant;
import dayutil;
Variant run(int part, File input, bool bigboy, string[] args) {
import std.conv;
auto numbers = input.byLineCopy.array.map!(to!uint);
return Variant(parts!uint(part,
() => part1(numbers)));
}
T part1(Range, T = ElementType!Range)(Range range, int preambleSize = 25)
if (isForwardRange!Range && isNumeric!T) {
foreach(window; range.slide!(No.withPartial)(preambleSize + 1)) {
if(!window[0..preambleSize].any!(e => window.canFind!(x => e + x == window[preambleSize]))) {
return window[preambleSize];
}
}
return -1;
}
unittest {
int[] numbers = [35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299,
277, 309, 576];
assert(part1(numbers, 5) == 127);
}