[2017][ruby][13.x] slowrun

sorbet
Alpha Chen 7 years ago
parent cb7d65b004
commit fc1e8e4c33

@ -0,0 +1,44 @@
0: 3
1: 2
2: 4
4: 8
6: 5
8: 6
10: 6
12: 4
14: 6
16: 6
18: 17
20: 8
22: 8
24: 8
26: 9
28: 8
30: 12
32: 12
34: 10
36: 12
38: 12
40: 8
42: 12
44: 12
46: 10
48: 12
50: 12
52: 14
54: 14
56: 12
58: 14
60: 14
62: 14
64: 14
66: 14
68: 12
70: 14
72: 14
74: 14
76: 14
80: 18
82: 14
90: 18

@ -0,0 +1,62 @@
input = ARGF.read.strip
# 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
end
def pass?(delay)
f = Firewall.new(@input).each
delay.times do
f.next
end
severity = 0
(0..@input.map(&:first).max).each do |pos|
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