Skip to content

Instantly share code, notes, and snippets.

@andrei512
Created April 25, 2015 08:16
Show Gist options
  • Save andrei512/9846e62ad2e17dc234ff to your computer and use it in GitHub Desktop.
Save andrei512/9846e62ad2e17dc234ff to your computer and use it in GitHub Desktop.
the ruby code that won the ccc
require 'socket'
MAX_DIST = 120
@s = TCPSocket.new 'localhost', 7000
def get_number_of_rods
@s.puts "GET_NUMBER"
@s.gets.split.map(&:to_i)
end
def get_center
@s.puts "GET_POSITION"
@s.gets.split.map(&:to_f)
end
n, m = get_number_of_rods
@n = n
@m = m
def all
positions = []
for i in 1..@n
for j in 1..@m
positions << [i, j]
end
end
positions
end
def even
positions = []
for i in 1..@n
for j in 1..@m
if (i ^ j) % 2 == 0
positions << [i, j]
end
end
end
positions
end
def odd
positions = []
for i in 1..@n
for j in 1..@m
if (i ^ j) % 2 == 1
positions << [i, j]
end
end
end
positions
end
# puts even.inspect
def move rods, deviations
cx, cy = get_center
@s.puts "MOVE " + rods.zip(deviations).select { |rod, dev|
rodi, rodj = rod
devx, devy = dev
px, py = rodi * 10, rodj * 10
dx = (cx - px).abs
dy = (cy - py).abs
dist = Math.sqrt(dx ** 2 + dy ** 2)
dist < MAX_DIST
}.map { |rod, dev|
rodi, rodj = rod
devx, devy = dev
"#{rodi} #{rodj} #{devx} #{devy}"
}.join(" ")
# puts
"step #{@s.gets}"
end
def step_2k x, y
puts "moving #{x}, #{y}"
# Lower even rod
move odd, odd.map { [0, 0]}
# Lift the lowered rods to position -k and lower the rods in between
move even, even.map {[-x, -y]}
# Move the lifted rods to position k
move even, even.map {[x, y]}
# Lift the lowered rods to position 0
move all, all.map { |pos|
i, j = pos
if (i ^ j) % 2 == 0
[x, y]
else
[0, 0]
end
}
# Lower the lifted rods which are in position k
move odd, odd.map{[0, 0]}
# Lift the lowered rods to position 0
move all, all.map{[0, 0]}
end
def go_to x, y
loop do
cx, cy = get_center
dx, dy = x - cx, y - cy
if dx.abs <= 3 and dy.abs <= 3
break
end
dx = 10 if dx > 10
dx = -10 if dx < -10
dy = 10 if dy > 10
dy = -10 if dy < -10
sx = ((dx + 1) / 2).to_i
sy = ((dy + 1) / 2).to_i
puts sx, sy
step_2k sx, sy
end
end
def around_shape
cx, cy = get_center
positions = []
for i in 1..@n
for j in 1..@m
x = i * 10
y = j * 10
dx = (cx - x).abs
dy = (cy - y).abs
dist = Math.sqrt(dx ** 2 + dy ** 2)
if dist < MAX_DIST
positions << [i, j]
end
end
end
positions
end
def above
cx, cy = get_center
around_shape.select { |x, y|
cy <= y * 10
}
end
def below
cx, cy = get_center
around_shape.select { |x, y|
cy > y * 10
}
end
class Array
def take_even
select { |x, y|
(x ^ y) % 2 == 1
}
end
def take_odd
select { |x, y|
(x ^ y) % 2 == 0
}
end
end
@cx, @cy = get_center
@cx += 60
def rotate_shape k
# Lower even rod
# move above.take_odd, above.take_odd.map {[0, 0]}
move above.take_odd + below.take_odd, above.take_odd.map {[0, 0]} + below.take_odd.map {[0, 0]}
# Lift the lowered rods to position -k and lower the rods in between
move above.take_odd + below.take_odd, above.take_odd.map {[-k, -k]} + below.take_odd.map {[k, k]}
# Move the lifted rods to position k
rods = above.take_odd + below.take_odd
devs = above.take_odd.map {[k, k]} + below.take_odd.map {[-k, -k]}
next_rods = above.take_even + below.take_even
next_devs = next_rods.map { [0, 0]}
move rods, devs
# Lift the lowered rods to position 0
move rods + next_rods, devs + next_devs
# Lower the lifted rods which are in position k
move next_rods, next_devs
# Lift the lowered rods to position 0
move around_shape, around_shape.map {[0, 0]}
end
25.times do |i|
puts "index = #{i}"
# puts "YOLO!"
5.times do
rotate_shape -5
end
# puts "SWAG!"
go_to @cx, @cy
end
@cx, @cy = get_center
@cy -= 60
go_to @cx, @cy
@s.puts "EXIT"
# 10.times do
# step_2k 5, 0
# end
# loop do
# step_2k 0, 5
# cx, cy = get_center
# puts "center = {#{cx}, #{cy}}"
# if cy == 200
# break
# end
# end
# PI = Math.atan2(0.0, -1.0)
# PI2 = 2 * PI
# angle = 180
# 365.times do
# angle += 1
# to_rad = angle * PI2 / 360.0
# x, y = Math.cos(to_rad), Math.sin(to_rad)
# gx = (x * 200 + 300).to_i
# gy = (y * 200 + 300).to_i
# # puts "#{gx}, #{gy}"
# go_to gx, gy
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment