aboutsummaryrefslogtreecommitdiff
path: root/day13/part2.dnw
diff options
context:
space:
mode:
authorNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-13 23:40:17 +0000
committerNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-13 23:40:17 +0000
commit3d1ca6fb0a6741799b1e03c3ac15d94b85facbd5 (patch)
tree4a47a6c22154a77cca5373ab49fa0574ecd710b0 /day13/part2.dnw
parentd011cdb55f13c55ffc6bb4b854ef30c2561f2a7c (diff)
Day13, Part2 does not work
Diffstat (limited to 'day13/part2.dnw')
-rwxr-xr-xday13/part2.dnw53
1 files changed, 53 insertions, 0 deletions
diff --git a/day13/part2.dnw b/day13/part2.dnw
new file mode 100755
index 0000000..af6af5b
--- /dev/null
+++ b/day13/part2.dnw
@@ -0,0 +1,53 @@
+#!/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)}
+lcm = input.map(&:last).inject(1, :lcm)
+delay = 0
+
+loop do
+ if delay > lcm then
+ puts "Went past lcm"
+ exit
+ end
+
+ firewall = Firewall.new
+
+ input.each do |line|
+ firewall.add_layer!(line[0], line[1])
+ end
+
+ break unless firewall.will_capture_at?(delay)
+
+ delay += 1
+end
+
+puts delay