mirror of
https://github.com/HenkKalkwater/aoc-2020
synced 2024-11-22 11:05:18 +00:00
Add day 9 part 1 and automatically open file for day
This commit is contained in:
parent
53c5a606ee
commit
3e2632b2af
14
source/app.d
14
source/app.d
|
@ -15,6 +15,7 @@ import day5;
|
||||||
import day6;
|
import day6;
|
||||||
import day7;
|
import day7;
|
||||||
import day8;
|
import day8;
|
||||||
|
import day9;
|
||||||
import dayutil;
|
import dayutil;
|
||||||
|
|
||||||
immutable string progName = "aoc-2020";
|
immutable string progName = "aoc-2020";
|
||||||
|
@ -28,6 +29,7 @@ Variant function(int, File, bool, string[])[] programs = [
|
||||||
&day6.run,
|
&day6.run,
|
||||||
&day7.run,
|
&day7.run,
|
||||||
&day8.run,
|
&day8.run,
|
||||||
|
&day9.run,
|
||||||
];
|
];
|
||||||
|
|
||||||
void printUsage(string name) {
|
void printUsage(string name) {
|
||||||
|
@ -71,15 +73,21 @@ void main(string[] args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
File file;
|
File file;
|
||||||
if (args.length < 4 || args[3] == "-") {
|
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;
|
file = stdin;
|
||||||
} else {
|
} else {
|
||||||
try {
|
|
||||||
file = File(args[3], "rb");
|
file = File(args[3], "rb");
|
||||||
|
}
|
||||||
} catch (ErrnoException e) {
|
} catch (ErrnoException e) {
|
||||||
printUsage(args[0], "Error %d while opening input file: %s".format(e.errno, e.message));
|
printUsage(args[0], "Error %d while opening input file: %s".format(e.errno, e.message));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Variant result = programs[day - 1](part, file, bigboy, args[3..$]);
|
Variant result = programs[day - 1](part, file, bigboy, args[3..$]);
|
||||||
|
|
31
source/day9.d
Normal file
31
source/day9.d
Normal 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);
|
||||||
|
}
|
Loading…
Reference in a new issue