aboutsummaryrefslogtreecommitdiff
path: root/day13/part1
blob: 9af71a68756adad170f79bdfa11c327eb7a5625a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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