# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1512665082 18000 # Node ID 0fa6cd63af216b121bece0aadac3baad1c9bbf8a day 1 diff --git a/2017/01/app.d b/2017/01/app.d new file mode 100644 --- /dev/null +++ b/2017/01/app.d @@ -0,0 +1,38 @@ +import std.getopt; +import std.stdio; +import std.conv: to; +import std.traits: isNumeric; + +numType day1(numType)(string input, size_t stride = 0) + if(isNumeric!numType) +{ + auto n = input.length; + if(stride == 0) { + stride = n/2; + } + int sum = 0; + foreach(idx, c; input) { + auto prev = input[(idx + stride)%n]; + if (c == prev) { + sum += to!(numType)(c - '0'); + } + } + return sum; +} + +void main(string[] args) { + string input; + auto opts = getopt( + args, + "input|i", "Input captcha to process", &input + ); + + if (opts.helpWanted) { + defaultGetoptPrinter("Day 1 of AoC", opts.options); + } + + auto result1 = day1!(int)(input, 1); + auto result2 = day1!(int)(input); + writeln(result1); + writeln(result2); +}