[2021][ruby][17.x] a bit of refactoring

pull/2/head
Alpha Chen 3 years ago
parent 8c5c67ae24
commit 169491113a

@ -1,33 +1,29 @@
class Probe def fire(v, target)
def initialize(velocity, target) return enum_for(__method__, v, target) unless block_given?
@velocity, @target = velocity, target
end
def each
return enum_for(__method__) unless block_given?
pos = [0,0] pos = [0,0]
velocity = @velocity.clone v_x, v_y = v
loop do loop do
yield pos yield pos
return if @target.zip(pos).all? { _1.cover?(_2) } return if target.zip(pos).all? { _1.cover?(_2) }
pos = pos.zip(velocity).map { _1.sum } pos = pos.zip([v_x, v_y]).map { _1.sum }
velocity[0] -= 1 if velocity[0] > 0 v_x -= 1 if v_x > 0
velocity[1] -= 1 v_y -= 1
return if pos[0] > @target[0].end return if pos[0] > target[0].end
return if pos[1] < @target[1].begin return if pos[1] < target[1].begin
end
end end
end end
input = ARGF.read.scan(/x=(.+), y=(.+)/) input = ARGF.read.scan(/x=(.+), y=(.+)/)
target = input[0].map { eval(_1) } target = input[0].map { eval(_1) }
haystack = (0..256).flat_map {|x| (-256..256).map {|y| [x, y] }} xs = (0..target[0].end).select { (1.._1).sum > target[0].begin }
haystack.select! {|v| Probe.new(v, target).each.any? {|pos| target.zip(pos).all? { _1.cover?(_2) }}} haystack = xs.flat_map {|x| (-256..256).map {|y| [x, y] }}
# p haystack.map {|v| Probe.new(v, target).each.map { _2 }.max }.max haystack.select! {|v| fire(v, target).each.any? {|pos| target.zip(pos).all? { _1.cover?(_2) }}}
# p haystack.map {|v| fire(v, target).each.map { _2 }.max }.max
p haystack.count p haystack.count

Loading…
Cancel
Save