Mercurial > hg > aoc
view 2017/day13.d @ 14:56b75b62d591
day 13
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Wed, 13 Dec 2017 23:51:28 -0500 |
parents | |
children | 817954f71d53 |
line wrap: on
line source
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); }