annotate 2017/day02.d @ 12:a991288f3d55 tip @

day 11: simplify hexNorm
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Mon, 11 Dec 2017 22:24:25 -0500
parents b5533de6ff5b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
1 import std.stdio;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
2 import std.array: array;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
3 import std.algorithm: map, maxElement, minElement;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
4 import std.string: split;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
5 import std.conv: to;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
6 import std.traits: isNumeric;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
7
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
8 auto tokenize(numType, lineType)(lineType line)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
9 if(isNumeric!numType)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
10 {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
11 return line.split.map!(x => to!numType(x)).array;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
12 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
13
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
14 numType part1(numType, lineType)(lineType spreadsheet)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
15 if(isNumeric!numType)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
16 {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
17 numType checksum = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
18 foreach(line; spreadsheet) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
19 checksum += line.maxElement - line.minElement;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
20 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
21 return checksum;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
22 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
23
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
24 numType part2(numType, lineType)(lineType spreadsheet)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
25 if(isNumeric!numType)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
26 {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
27 numType checksum = 0;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
28 foreach(line; spreadsheet) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
29 nextline:
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
30 foreach(idx, x; line) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
31 foreach (y; line[idx+1..$]) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
32 if (x % y == 0) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
33 checksum += x/y;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
34 continue nextline;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
35 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
36 if (y % x == 0) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
37 checksum += y/x;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
38 continue nextline;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
39 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
40 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
41 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
42 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
43 return checksum;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
44 }
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
45
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
46 void main(string[] args) {
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
47 alias numType = int;
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
48 auto spreadsheet =
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
49 File(args[1])
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
50 .byLineCopy
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
51 .array
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
52 .map!(line => tokenize!numType(line));
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
53 auto result1 = part1!numType(spreadsheet);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
54 auto result2 = part2!numType(spreadsheet);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
55 writeln(result1);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
56 writeln(result2);
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
57 }