aboutsummaryrefslogtreecommitdiff
path: root/day13/part2
diff options
context:
space:
mode:
authorNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-14 18:33:27 +0000
committerNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-14 18:33:27 +0000
commit697558abc7277dc476e5e63f843e26f4b7f4eb4e (patch)
treef4df8697e1d359c3f0c15df4c438fe7df6a171f6 /day13/part2
parentee1a8bf1499e297ba1a2386ed68cbca2270cebbe (diff)
Day13 Part2 complete
Diffstat (limited to 'day13/part2')
-rwxr-xr-xday13/part242
1 files changed, 42 insertions, 0 deletions
diff --git a/day13/part2 b/day13/part2
new file mode 100755
index 0000000..71a8ccd
--- /dev/null
+++ b/day13/part2
@@ -0,0 +1,42 @@
+#!/usr/bin/env ruby
+
+class Firewall
+ class Layer
+ def initialize(range)
+ @period = (range - 1) * 2
+ end
+
+ def will_capture_at?(time)
+ return time % @period == 0
+ end
+ end
+
+ def initialize
+ @layers = []
+ end
+
+ def add_layer!(depth, range)
+ @layers[depth] = Layer.new(range)
+ end
+
+ def will_capture_at?(time)
+ (0...@layers.length).each do |i|
+ next if @layers[i].nil?
+ return true if @layers[i].will_capture_at?(time + i)
+ end
+ return false
+ end
+end
+
+
+input = $stdin.readlines.map(&:chomp).map{|l|l.split(': ').map(&:to_i)}
+
+firewall = Firewall.new
+input.each do |line|
+ firewall.add_layer!(line[0], line[1])
+end
+
+delay = 0
+delay += 1 while firewall.will_capture_at?(delay)
+
+puts delay