# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1512771683 18000 # Node ID 7d06d033b0ee4f9b1685973577e200f7bba99120 # Parent 52fdca7ea9bef8707a24bb6c6aa91f8fea8cf519 day 8 diff --git a/2017/day08.d b/2017/day08.d new file mode 100644 --- /dev/null +++ b/2017/day08.d @@ -0,0 +1,42 @@ +import std.algorithm: max, maxElement; +import std.regex; +import std.stdio; +import std.conv: to; + +int[string] registers; +int maxSoFar = 0; + +void evalInstruction(string line) { + auto comparisons = [ + "<": function(int a, int b) => a < b, + ">": function(int a, int b) => a > b, + "==": function(int a, int b) => a == b, + "<=": function(int a, int b) => a <= b, + ">=": function(int a, int b) => a >= b, + "!=": function(int a, int b) => a != b, + ]; + static instructionRegex = regex( + r"(?P\w+) (?Pinc|dec) (?P-?\d+) " + ~ r"if (?P\w+) (?P[=<>!]{1,2}) (?P-?\d+)" + ); + auto row = matchFirst(line, instructionRegex); + if(comparisons[row["comp"]]( + registers.get(row["condreg"], 0), + to!int(row["amtcond"]))){ + if(row["op"] == "inc") { + registers[row["reg"]] = registers.get(row["reg"], 0) + to!int(row["amt"]); + } + else if(row["op"] == "dec") { + registers[row["reg"]] = registers.get(row["reg"], 0) - to!int(row["amt"]); + } + } + maxSoFar = max(maxSoFar, registers.values.maxElement); +} + +void main(string[] args) { + foreach(line; File(args[1]).byLineCopy) { + evalInstruction(line); + } + writeln(registers.values.maxElement); + writeln(maxSoFar); +}