public
Created

  • Download Gist
gistfile1.rb
Ruby
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
class Numeric
def degrees
self * Math::PI / 180
end
end
 
module RobotJoust
 
TIME_PRECISION = 0.1
BOUNCE_FALLOFF = 0.9 # -10%
 
class Robot
RADIUS = 250
attr_accessor :position, :vector, :speed
 
def initialize position, angle, initial_speed
@speed = initial_speed
@pos_x = position[:x]
@pos_y = position[:y]
@vector = {
x: (Math.cos(angle.degrees) * @speed * RobotJoust::TIME_PRECISION),
y: (Math.sin(-angle.degrees) * @speed * RobotJoust::TIME_PRECISION)
}
end
 
def move bounds
@pos_x += @vector[:x]
new_pos_y = (@pos_y + @vector[:y])
 
if 0 >= new_pos_y - RADIUS or new_pos_y + RADIUS >= bounds[:height]
bounce bounds
else
@pos_y += @vector[:y]
end
end
 
def bounce bounds
# top bounce
if @vector[:y] >= 0
dist_from_bounce = (bounds[:height] - @pos_y) - RADIUS
dist_after_bounce = (@vector[:y] - dist_from_bounce) * RobotJoust::BOUNCE_FALLOFF
@pos_y = bounds[:height] - RADIUS - dist_after_bounce
# bottom bounce
else
dist_from_bounce = @pos_y - RADIUS
dist_after_bounce = (-@vector[:y] - dist_from_bounce) * RobotJoust::BOUNCE_FALLOFF
@pos_y = RADIUS + dist_after_bounce
end
 
@vector[:y] *= -RobotJoust::BOUNCE_FALLOFF
@vector[:x] *= RobotJoust::BOUNCE_FALLOFF
@speed *= RobotJoust::BOUNCE_FALLOFF
end
 
def position
{x: @pos_x, y: @pos_y}
end
 
def to_s
"Robot: #{position.to_s} | #{@vector}"
end
end
 
class Arena
attr_accessor :bounds, :robots
 
def initialize(width, height)
@cycles = 0
@robots = []
@bounds = {width: width, height: height}
end
 
def add_robot(robot)
@robots << robot
end
 
def check_collisions!
@robots.each do |robot|
if robot.position[:x] < 0 or robot.position[:x] > @bounds[:width]
puts "No winner found"
return true
end
end
 
x_dist = @robots[0].position[:x] - @robots[1].position[:x]
y_dist = @robots[0].position[:y] - @robots[1].position[:y]
if Math.sqrt(x_dist * x_dist + y_dist * y_dist) <= 2*RobotJoust::Robot::RADIUS
if @robots[0].speed > @robots[1].speed
puts "Left robot wins at #{elapsed_time} seconds."
else
puts "Right robot wins at #{elapsed_time} seconds."
end
return true
end
 
false
end
 
def joust!
while true
@robots.each do |robot|
robot.move(@bounds)
end
break if check_collisions!
@cycles += 1
end
end
 
def elapsed_time
@cycles * RobotJoust::TIME_PRECISION
end
 
def to_s
"Arena: #{@bounds.to_s}"
end
 
end
 
class Match
def initialize input
lines = input
 
arena_details = lines[0].split(" ").map(&:to_i)
@arena = RobotJoust::Arena.new(arena_details[0] * 1000, arena_details[1] * 1000)
 
left_robot_details = lines[1].split(" ").map(&:to_i)
@arena.add_robot(RobotJoust::Robot.new(
{
x: 0,
y: @arena.bounds[:height]/2
},
left_robot_details[0],
left_robot_details[1])
)
 
right_robot_details = lines[2].split(" ").map(&:to_i)
@arena.add_robot(RobotJoust::Robot.new(
{
x: @arena.bounds[:width],
y: @arena.bounds[:height]/2
},
right_robot_details[0] + 180,
right_robot_details[1])
)
 
end
 
def play!
@arena.joust!
end
end
 
end
 
match = RobotJoust::Match.new(readlines)
match.play!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.