Mercurial > hg > aoc
changeset 11:66707d1400b4
day 11
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Mon, 11 Dec 2017 21:59:08 -0500 |
parents | bd993cf00e2f |
children | a991288f3d55 |
files | 2017/day11.d |
diffstat | 1 files changed, 37 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
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); +}