comparison 2017/day08/app.d @ 35:1d99d733cf13 default tip @

day08: replace static foreach with workaround
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 16 Jan 2018 11:28:55 -0500
parents bc652fa0a645
children
comparison
equal deleted inserted replaced
34:049fb8e56025 35:1d99d733cf13
3 import std.stdio; 3 import std.stdio;
4 import std.conv: to; 4 import std.conv: to;
5 5
6 int[string] registers; 6 int[string] registers;
7 int maxSoFar = 0; 7 int maxSoFar = 0;
8 bool function(int,int)[string] comparisons;
8 9
9 bool function(int,int)[string] comparisons; 10 auto getComparisons(Args...)() {
10 static foreach(cmp; ["<", ">", "==", "<=", ">=", "!="]) { 11 foreach(cmp; Args) {
11 comparisons[cmp] = mixin("function(int a, int b) => a "~cmp~" b"); 12 comparisons[cmp] = mixin("function(int a, int b) => a "~cmp~" b");
13 }
14 return comparisons;
15 }
16
17 shared static this() {
18 comparisons = getComparisons!("<", ">", "==", "<=", ">=", "!=");
12 } 19 }
13 20
14 void evalInstruction(string line) { 21 void evalInstruction(string line) {
15 static instructionRegex = regex( 22 static instructionRegex = regex(
16 r"(?P<reg>\w+) (?P<op>inc|dec) (?P<amt>-?\d+) " 23 r"(?P<reg>\w+) (?P<op>inc|dec) (?P<amt>-?\d+) "
29 } 36 }
30 maxSoFar = max(maxSoFar, registers.values.maxElement); 37 maxSoFar = max(maxSoFar, registers.values.maxElement);
31 } 38 }
32 39
33 void main(string[] args) { 40 void main(string[] args) {
34
35 foreach(line; File(args[1]).byLineCopy) { 41 foreach(line; File(args[1]).byLineCopy) {
36 evalInstruction(line); 42 evalInstruction(line);
37 } 43 }
38 writeln(registers.values.maxElement); 44 writeln(registers.values.maxElement);
39 writeln(maxSoFar); 45 writeln(maxSoFar);