diff options
Diffstat (limited to 'day13')
-rw-r--r-- | day13/input | 43 | ||||
-rwxr-xr-x | day13/part1 | 70 | ||||
-rwxr-xr-x | day13/part2.dnw | 53 | ||||
-rw-r--r-- | day13/test | 4 |
4 files changed, 170 insertions, 0 deletions
diff --git a/day13/input b/day13/input new file mode 100644 index 0000000..a9a659c --- /dev/null +++ b/day13/input @@ -0,0 +1,43 @@ +0: 3 +1: 2 +2: 4 +4: 4 +6: 5 +8: 8 +10: 6 +12: 6 +14: 6 +16: 6 +18: 8 +20: 8 +22: 12 +24: 10 +26: 9 +28: 8 +30: 8 +32: 12 +34: 12 +36: 12 +38: 12 +40: 8 +42: 12 +44: 14 +46: 14 +48: 10 +50: 12 +52: 12 +54: 14 +56: 14 +58: 14 +62: 12 +64: 14 +66: 14 +68: 14 +70: 12 +74: 14 +76: 14 +78: 14 +80: 18 +82: 17 +84: 30 +88: 14 diff --git a/day13/part1 b/day13/part1 new file mode 100755 index 0000000..9af71a6 --- /dev/null +++ b/day13/part1 @@ -0,0 +1,70 @@ +#!/usr/bin/env ruby + +class Firewall + class Layer + attr_reader :range + + def initialize(range) + @scanner_location = 0 + @scanner_velocity = 1 + @range = range + end + + def move_scanner! + if @scanner_location == (@range - 1) then + @scanner_velocity = -1 + elsif @scanner_location == 0 then + @scanner_velocity = 1 + end + + @scanner_location += @scanner_velocity + end + + def capturing? + return @scanner_location == 0 + end + end + + def initialize + @layers = [] + end + + def add_layer!(depth, range) + @layers[depth] = Layer.new(range) + end + + def total_depth + return @layers.length + end + + def severity(loc) + return 0 if @layers[loc].nil? + return 0 unless @layers[loc].capturing? + return loc * @layers[loc].range + end + + def tick! + @layers.compact.each(&:move_scanner!) + end +end + + +input = $stdin.readlines.map(&:chomp) + +firewall = Firewall.new + +input.each do |line| + depth, range = line.split(': ').map(&:to_i) + firewall.add_layer!(depth, range) +end + +myloc = -1 +total_severity = 0 + +while myloc < firewall.total_depth do + myloc += 1 + total_severity += firewall.severity(myloc) + firewall.tick! +end + +puts total_severity 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 diff --git a/day13/test b/day13/test new file mode 100644 index 0000000..0239024 --- /dev/null +++ b/day13/test @@ -0,0 +1,4 @@ +0: 3 +1: 2 +4: 4 +6: 4 |