Mercurial > hg > aoc
annotate 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 |
rev | line source |
---|---|
8 | 1 import std.algorithm: max, maxElement; |
2 import std.regex; | |
3 import std.stdio; | |
4 import std.conv: to; | |
5 | |
6 int[string] registers; | |
7 int maxSoFar = 0; | |
35
1d99d733cf13
day08: replace static foreach with workaround
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
33
diff
changeset
|
8 bool function(int,int)[string] comparisons; |
8 | 9 |
35
1d99d733cf13
day08: replace static foreach with workaround
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
33
diff
changeset
|
10 auto getComparisons(Args...)() { |
1d99d733cf13
day08: replace static foreach with workaround
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
33
diff
changeset
|
11 foreach(cmp; Args) { |
1d99d733cf13
day08: replace static foreach with workaround
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
33
diff
changeset
|
12 comparisons[cmp] = mixin("function(int a, int b) => a "~cmp~" b"); |
1d99d733cf13
day08: replace static foreach with workaround
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
33
diff
changeset
|
13 } |
1d99d733cf13
day08: replace static foreach with workaround
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
33
diff
changeset
|
14 return comparisons; |
1d99d733cf13
day08: replace static foreach with workaround
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
33
diff
changeset
|
15 } |
1d99d733cf13
day08: replace static foreach with workaround
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
33
diff
changeset
|
16 |
1d99d733cf13
day08: replace static foreach with workaround
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
33
diff
changeset
|
17 shared static this() { |
1d99d733cf13
day08: replace static foreach with workaround
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
33
diff
changeset
|
18 comparisons = getComparisons!("<", ">", "==", "<=", ">=", "!="); |
33
bc652fa0a645
Move all solutions to per-day subdirs
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
8
diff
changeset
|
19 } |
bc652fa0a645
Move all solutions to per-day subdirs
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
8
diff
changeset
|
20 |
8 | 21 void evalInstruction(string line) { |
22 static instructionRegex = regex( | |
23 r"(?P<reg>\w+) (?P<op>inc|dec) (?P<amt>-?\d+) " | |
24 ~ r"if (?P<condreg>\w+) (?P<comp>[=<>!]{1,2}) (?P<amtcond>-?\d+)" | |
25 ); | |
26 auto row = matchFirst(line, instructionRegex); | |
27 if(comparisons[row["comp"]]( | |
28 registers.get(row["condreg"], 0), | |
29 to!int(row["amtcond"]))){ | |
30 if(row["op"] == "inc") { | |
31 registers[row["reg"]] = registers.get(row["reg"], 0) + to!int(row["amt"]); | |
32 } | |
33 else if(row["op"] == "dec") { | |
34 registers[row["reg"]] = registers.get(row["reg"], 0) - to!int(row["amt"]); | |
35 } | |
36 } | |
37 maxSoFar = max(maxSoFar, registers.values.maxElement); | |
38 } | |
39 | |
40 void main(string[] args) { | |
41 foreach(line; File(args[1]).byLineCopy) { | |
42 evalInstruction(line); | |
43 } | |
44 writeln(registers.values.maxElement); | |
45 writeln(maxSoFar); | |
46 } |