# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1513227088 18000 # Node ID 56b75b62d5913a834473ab3518a87631b85ef363 # Parent 13f69301183f817bef150dbd90eca9bec5ed0c30 day 13 diff --git a/2017/day13.d b/2017/day13.d new file mode 100644 --- /dev/null +++ b/2017/day13.d @@ -0,0 +1,29 @@ +import std.stdio; +import std.algorithm: filter, map, sum; +import std.string: split; +import std.conv: to; +import std.array: array; + +auto getHits(T)(T firewall, int init = 0) { + return firewall.filter!(xy => (init + xy[0]) % (2*(xy[1]-1)) == 0); +} + +auto computeSeverity(T)(T firewall) { + return firewall.getHits.map!(xy => xy[0]*xy[1]).sum; +} + +auto findPassing(T)(T firewall) { + auto init = 0; + while(! firewall.getHits(init).empty) { + init++; + } + return init; +} + +void main(string[] args) { + auto firewall = File(args[1]).byLineCopy.map!( + x => x.split(": ").map!(to!int) + ).array; + writeln(firewall.computeSeverity); + writeln(firewall.findPassing); +}