Mercurial > hg > aoc
comparison 2017/day02.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/02/app.d@772b9f146b20 |
children |
comparison
equal
deleted
inserted
replaced
2:4620ff0a058c | 3:b5533de6ff5b |
---|---|
1 import std.stdio; | |
2 import std.array: array; | |
3 import std.algorithm: map, maxElement, minElement; | |
4 import std.string: split; | |
5 import std.conv: to; | |
6 import std.traits: isNumeric; | |
7 | |
8 auto tokenize(numType, lineType)(lineType line) | |
9 if(isNumeric!numType) | |
10 { | |
11 return line.split.map!(x => to!numType(x)).array; | |
12 } | |
13 | |
14 numType part1(numType, lineType)(lineType spreadsheet) | |
15 if(isNumeric!numType) | |
16 { | |
17 numType checksum = 0; | |
18 foreach(line; spreadsheet) { | |
19 checksum += line.maxElement - line.minElement; | |
20 } | |
21 return checksum; | |
22 } | |
23 | |
24 numType part2(numType, lineType)(lineType spreadsheet) | |
25 if(isNumeric!numType) | |
26 { | |
27 numType checksum = 0; | |
28 foreach(line; spreadsheet) { | |
29 nextline: | |
30 foreach(idx, x; line) { | |
31 foreach (y; line[idx+1..$]) { | |
32 if (x % y == 0) { | |
33 checksum += x/y; | |
34 continue nextline; | |
35 } | |
36 if (y % x == 0) { | |
37 checksum += y/x; | |
38 continue nextline; | |
39 } | |
40 } | |
41 } | |
42 } | |
43 return checksum; | |
44 } | |
45 | |
46 void main(string[] args) { | |
47 alias numType = int; | |
48 auto spreadsheet = | |
49 File(args[1]) | |
50 .byLineCopy | |
51 .array | |
52 .map!(line => tokenize!numType(line)); | |
53 auto result1 = part1!numType(spreadsheet); | |
54 auto result2 = part2!numType(spreadsheet); | |
55 writeln(result1); | |
56 writeln(result2); | |
57 } |