annotate 2017/day01.d @ 13:13f69301183f tip @

day 12
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 12 Dec 2017 17:21:08 -0500
parents b5533de6ff5b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
1 import std.getopt;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
2 import std.stdio;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
3 import std.conv: to;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
4 import std.traits: isNumeric;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
5
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
6 numType day1(numType)(string input, size_t stride = 0)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
7 if(isNumeric!numType)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
8 {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
9 auto n = input.length;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
10 if(stride == 0) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
11 stride = n/2;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
12 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
13 int sum = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
14 foreach(idx, c; input) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
15 auto prev = input[(idx + stride)%n];
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
16 if (c == prev) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
17 sum += to!(numType)(c - '0');
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
18 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
19 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
20 return sum;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
21 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
22
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
23 void main(string[] args) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
24 string input;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
25 auto opts = getopt(
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
26 args,
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
27 "input|i", "Input captcha to process", &input
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
28 );
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
29
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
30 if (opts.helpWanted) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
31 defaultGetoptPrinter("Day 1 of AoC", opts.options);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
32 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
33
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
34 auto result1 = day1!(int)(input, 1);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
35 auto result2 = day1!(int)(input);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
36 writeln(result1);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
37 writeln(result2);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
38 }