public int GetDelayForNoAlarms(Firewall firewall) { int delay = 0; bool safe = false; while (!safe) { bool earlyBreak = false; for (int i = 0; i < firewall.Depth(); i++) { if (firewall.AlarmWillBeTriggered(i, i + delay)) { // This delay is no good, so increase the delay and try again. delay++; earlyBreak = true; break; } } // If at no point we terminated the loop early, then every layer will // be safe as we travel through, so we've found the necessary delay. if (!earlyBreak) { safe = true; } } return(delay); }
public int GetTravelCost(Firewall firewall) { int cost = 0; for (int i = 0; i < firewall.Depth(); i++) { // Each time the alarm will be triggered by us moving into it, // we increment the cost by the depth into the firewall we're at // times the depth of the layer we are entering. if (firewall.AlarmWillBeTriggered(i, i)) { cost += i * firewall.RangeAtDepth(i); } } return(cost); }
public void CreateFirewallsFromStrings() { var input = @"0: 3 1: 2 4: 4 6: 4"; Firewall f = FirewallFactory.CreateFromString(input); Assert.Equal(7, f.Depth()); Assert.Equal(3, f.RangeAtDepth(0)); Assert.Equal(2, f.RangeAtDepth(1)); Assert.Equal(1, f.RangeAtDepth(2)); Assert.Equal(1, f.RangeAtDepth(3)); Assert.Equal(4, f.RangeAtDepth(4)); Assert.Equal(1, f.RangeAtDepth(5)); Assert.Equal(4, f.RangeAtDepth(6)); }