Mercurial > hg > aoc
comparison 2017/day13.d @ 15:817954f71d53
day 13: cache a calculation for speed
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Thu, 14 Dec 2017 10:36:38 -0500 |
parents | 56b75b62d591 |
children |
comparison
equal
deleted
inserted
replaced
14:56b75b62d591 | 15:817954f71d53 |
---|---|
3 import std.string: split; | 3 import std.string: split; |
4 import std.conv: to; | 4 import std.conv: to; |
5 import std.array: array; | 5 import std.array: array; |
6 | 6 |
7 auto getHits(T)(T firewall, int init = 0) { | 7 auto getHits(T)(T firewall, int init = 0) { |
8 return firewall.filter!(xy => (init + xy[0]) % (2*(xy[1]-1)) == 0); | 8 return firewall.filter!(xy => (init + xy[0]) % xy[2] == 0); |
9 } | 9 } |
10 | 10 |
11 auto computeSeverity(T)(T firewall) { | 11 auto computeSeverity(T)(T firewall) { |
12 return firewall.getHits.map!(xy => xy[0]*xy[1]).sum; | 12 return firewall.getHits.map!(xy => xy[0]*xy[1]).sum; |
13 } | 13 } |
20 return init; | 20 return init; |
21 } | 21 } |
22 | 22 |
23 void main(string[] args) { | 23 void main(string[] args) { |
24 auto firewall = File(args[1]).byLineCopy.map!( | 24 auto firewall = File(args[1]).byLineCopy.map!( |
25 x => x.split(": ").map!(to!int) | 25 function(string x){ |
26 auto v = x.split(": ").map!(to!int); | |
27 return v.array ~ (2*v[1] - 2); | |
28 } | |
26 ).array; | 29 ).array; |
27 writeln(firewall.computeSeverity); | 30 writeln(firewall.computeSeverity); |
28 writeln(firewall.findPassing); | 31 writeln(firewall.findPassing); |
29 } | 32 } |