# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1512665091 18000 # Node ID 772b9f146b207ffc9cbe79700671af394bfd9e0d # Parent 0fa6cd63af216b121bece0aadac3baad1c9bbf8a day 2 diff --git a/2017/02/app.d b/2017/02/app.d new file mode 100644 --- /dev/null +++ b/2017/02/app.d @@ -0,0 +1,57 @@ +import std.stdio; +import std.array: array; +import std.algorithm: map, maxElement, minElement; +import std.string: split; +import std.conv: to; +import std.traits: isNumeric; + +auto tokenize(numType, lineType)(lineType line) + if(isNumeric!numType) +{ + return line.split.map!(x => to!numType(x)).array; +} + +numType part1(numType, lineType)(lineType spreadsheet) + if(isNumeric!numType) +{ + numType checksum = 0; + foreach(line; spreadsheet) { + checksum += line.maxElement - line.minElement; + } + return checksum; +} + +numType part2(numType, lineType)(lineType spreadsheet) + if(isNumeric!numType) +{ + numType checksum = 0; + foreach(line; spreadsheet) { + nextline: + foreach(idx, x; line) { + foreach (y; line[idx+1..$]) { + if (x % y == 0) { + checksum += x/y; + continue nextline; + } + if (y % x == 0) { + checksum += y/x; + continue nextline; + } + } + } + } + return checksum; +} + +void main(string[] args) { + alias numType = int; + auto spreadsheet = + File(args[1]) + .byLineCopy + .array + .map!(line => tokenize!numType(line)); + auto result1 = part1!numType(spreadsheet); + auto result2 = part2!numType(spreadsheet); + writeln(result1); + writeln(result2); +}