comparison 2017/day01.d @ 3:b5533de6ff5b

move all app.d's to top level
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Thu, 07 Dec 2017 11:44:52 -0500
parents 2017/01/app.d@0fa6cd63af21
children
comparison
equal deleted inserted replaced
2:4620ff0a058c 3:b5533de6ff5b
1 import std.getopt;
2 import std.stdio;
3 import std.conv: to;
4 import std.traits: isNumeric;
5
6 numType day1(numType)(string input, size_t stride = 0)
7 if(isNumeric!numType)
8 {
9 auto n = input.length;
10 if(stride == 0) {
11 stride = n/2;
12 }
13 int sum = 0;
14 foreach(idx, c; input) {
15 auto prev = input[(idx + stride)%n];
16 if (c == prev) {
17 sum += to!(numType)(c - '0');
18 }
19 }
20 return sum;
21 }
22
23 void main(string[] args) {
24 string input;
25 auto opts = getopt(
26 args,
27 "input|i", "Input captcha to process", &input
28 );
29
30 if (opts.helpWanted) {
31 defaultGetoptPrinter("Day 1 of AoC", opts.options);
32 }
33
34 auto result1 = day1!(int)(input, 1);
35 auto result2 = day1!(int)(input);
36 writeln(result1);
37 writeln(result2);
38 }