# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1513047548 18000 # Node ID 66707d1400b460b2f4bcf047528bbcd039f90285 # Parent bd993cf00e2f011c84379fe09a0b7921643c3b9e day 11 diff --git a/2017/day11.d b/2017/day11.d new file mode 100644 --- /dev/null +++ b/2017/day11.d @@ -0,0 +1,37 @@ +import std.array: array; +import std.math: abs; +import std.string: chomp, split; +import std.algorithm: min, map, cumulativeFold, maxElement; +import std.stdio; + +immutable int[2][string] directions; + +pure static this(){ + directions = [ + "ne": [1,1], + "n": [0,1], + "nw": [-1,0], + "sw": [-1,-1], + "s": [0,-1], + "se": [1,0], + ]; +} + +auto hexNorm(int x, int y) { + if (x*y < 0) { + return abs(x) + abs(y); + } + x = abs(x); + y = abs(y); + return abs(x - y) + min(x, y); +} + +void main(string[] args) { + auto norms = + File(args[1]).readln.chomp.split(",").map!(x => directions[x]) + .cumulativeFold!((a, b) => [a[0] + b[0], a[1] + b[1]])([0, 0]) + .map!(xy => hexNorm(xy[0], xy[1])) + .array; + writeln(norms[$-1]); + writeln(norms.maxElement); +}