diff options
Diffstat (limited to 'day13/part2')
-rwxr-xr-x | day13/part2 | 42 |
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 |