Mercurial > hg > aoc
comparison 2017/day08/app.d @ 33:bc652fa0a645
Move all solutions to per-day subdirs
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Tue, 09 Jan 2018 21:50:37 -0500 |
parents | 2017/day08.d@7d06d033b0ee |
children | 1d99d733cf13 |
comparison
equal
deleted
inserted
replaced
32:763c88851b91 | 33:bc652fa0a645 |
---|---|
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; | |
8 | |
9 bool function(int,int)[string] comparisons; | |
10 static foreach(cmp; ["<", ">", "==", "<=", ">=", "!="]) { | |
11 comparisons[cmp] = mixin("function(int a, int b) => a "~cmp~" b"); | |
12 } | |
13 | |
14 void evalInstruction(string line) { | |
15 static instructionRegex = regex( | |
16 r"(?P<reg>\w+) (?P<op>inc|dec) (?P<amt>-?\d+) " | |
17 ~ r"if (?P<condreg>\w+) (?P<comp>[=<>!]{1,2}) (?P<amtcond>-?\d+)" | |
18 ); | |
19 auto row = matchFirst(line, instructionRegex); | |
20 if(comparisons[row["comp"]]( | |
21 registers.get(row["condreg"], 0), | |
22 to!int(row["amtcond"]))){ | |
23 if(row["op"] == "inc") { | |
24 registers[row["reg"]] = registers.get(row["reg"], 0) + to!int(row["amt"]); | |
25 } | |
26 else if(row["op"] == "dec") { | |
27 registers[row["reg"]] = registers.get(row["reg"], 0) - to!int(row["amt"]); | |
28 } | |
29 } | |
30 maxSoFar = max(maxSoFar, registers.values.maxElement); | |
31 } | |
32 | |
33 void main(string[] args) { | |
34 | |
35 foreach(line; File(args[1]).byLineCopy) { | |
36 evalInstruction(line); | |
37 } | |
38 writeln(registers.values.maxElement); | |
39 writeln(maxSoFar); | |
40 } |