[2017][ruby][13.x] refactored

sorbet
Alpha Chen 7 years ago
parent fc1e8e4c33
commit 91cfc13777

@ -1,62 +1,11 @@
input = ARGF.read.strip firewall = Hash[ARGF.read.strip.scan(/(\d+): (\d+)/).map {|x| x.map(&:to_i) }]
# input = <<INPUT
# 0: 3
# 1: 2
# 4: 4
# 6: 4
# INPUT
@input = Hash[input.scan(/(\d+): (\d+)/).map {|x| x.map(&:to_i) }]
class Firewall
def initialize(input)
@layers = input
@scanners = Hash[input.map {|d,_| [d, 0] }]
@deltas = Hash[input.map {|d,_| [d, 1] }]
end
def each
return enum_for(__method__) unless block_given?
loop do
yield @scanners
@layers.each do |depth, range|
@scanners[depth] += @deltas[depth]
@deltas[depth] = case @scanners[depth]
when 0
1
when range - 1
-1
else
@deltas[depth]
end
end
end
end
def caught?(time, range)
(time % (2 * (range - 1))).zero?
end end
def pass?(delay) p firewall.select {|depth, range| caught?(depth, range) }.map {|d,r| d * r }.sum
f = Firewall.new(@input).each
delay.times do
f.next
end
severity = 0 p (1..Float::INFINITY).find {|delay|
(0..@input.map(&:first).max).each do |pos| firewall.none? {|depth, range| caught?(delay+depth, range) }
scanners = f.next }
return false if scanners[pos] == 0
end
true
end
def s(t, r)
r - (t % (2*r) - r).abs
end
(0..Float::INFINITY).each do |delay|
if @input.all? {|d,r| s(d + delay, r-1) != 0 }
p delay
exit
end
end

Loading…
Cancel
Save