Skip to content

Instantly share code, notes, and snippets.

@dukeimg
Last active April 22, 2016 03:32
Show Gist options
  • Save dukeimg/a630d03ad3e74da9cd717f2a06b81a46 to your computer and use it in GitHub Desktop.
Save dukeimg/a630d03ad3e74da9cd717f2a06b81a46 to your computer and use it in GitHub Desktop.
Решения codingame
STDOUT.sync = true # DO NOT REMOVE
# Don't let the machines win. You are humanity's last hope...
@width = gets.to_i # the number of cells on the X axis
@height = gets.to_i # the number of cells on the Y axis
cells = Array.new
@height.times do
line = gets.chomp # width characters, each either 0 or .
cells << line.split('') # заполняем ряды ячейками
end
def node(cell) # проверяем ячейку на узел
if cell == '0'
true
else
false
end
end
# row - номер ряда ряд
# cell - номер ячейки
@height.times do |row|
@width.times do |cell|
if node(cells[row][cell]) # ищем узел
coord1 = cell.to_s + ' ' + row.to_s # записываем его координату
if (cell + 1) == @width # если ячейка последняя в ряде
coord2 = '-1 -1' # то у неё нет соседа справа
else
1.upto(@width - cell) do |shift| # просматриваем ряд до конца в поисках узла
# shif - смезение поиска
if node(cells[row][cell + shift])
coord2 = (cell + shift).to_s + ' ' + row.to_s
break # выход из цикла поиска
end
end
end
if coord2 == nil # если ничего не нашли
coord2 = (cell + 1).to_s + ' ' + row.to_s
end
if (row + 1) == @height # если ниже уже некуда
coord3 = '-1 -1'
else
1.upto(@height - row) do |shift| # просматриваем столбец
if cells[row + shift] == nil # ряда нет
coord3 = cell.to_s + ' ' + (row + 1).to_s
break
elsif node(cells[row + shift][cell])
coord3 = cell.to_s + ' ' + (row + shift).to_s
break
end
end
end
puts coord1 + ' ' + coord2 + ' ' + coord3
end
end
end
# Write an action using puts
# To debug: STDERR.puts "Debug messages..."
# Three coordinates: a node, its right neighbor, its bottom neighbor
STDOUT.sync = true # DO NOT REMOVE
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
@road = gets.to_i # the length of the road before the gap.
@gap = gets.to_i # the length of the gap.
@platform = gets.to_i # the length of the landing platform.
required_speed = @gap + 1
# game loop
loop do
speed = gets.to_i # the motorbike's speed.
coord_x = gets.to_i # the position on the road of the motorbike.
# Write an action using puts
# To debug: STDERR.puts "Debug messages..."
action = "WAIT"
if speed < required_speed
action = "SPEED"
end
if speed > required_speed
action = "SLOW"
end
if (@road - coord_x) < speed
# Из-за огромной задержки приходтся
# рассчитывать оптимальный момент п
# рыжка отталкиваясь от скорости
action = "JUMP"
end
if coord_x >= @road + @gap
# Момент приземления
action = "SLOW"
end
# A single line containing one of 4 keywords: SPEED, SLOW, JUMP, WAIT.
puts action
end
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
@n = gets.to_i # the number of temperatures to analyse
@temps = gets.chomp # the n temperatures expressed as integers ranging from -273 to 5526
if @n == 0
puts 0
return
end
# Write an action using puts
# To debug: STDERR.puts "Debug messages..."
closest = 5526
arr = @temps.split(" ").map { |s| s.to_i }
arr.each do |t|
if t.abs < closest.abs
closest = t
elsif t.abs == closest.abs && t > 0
closest = t
end
end
puts closest
STDOUT.sync = true # DO NOT REMOVE
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
# game loop
loop do
highest = 0
index = 0
8.times do |n|
mountain_h = gets.to_i # represents the height of one mountain, from 9 to 0.
if mountain_h > highest
highest = mountain_h
index = n
end
end
# Write an action using puts
# To debug: STDERR.puts "Debug messages..."
puts index # The number of the mountain to fire on.
end
STDOUT.sync = true # DO NOT REMOVE
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
# ---
# Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
# light_x: the X position of the light of power
# light_y: the Y position of the light of power
# initial_tx: Thor's starting X position
# initial_ty: Thor's starting Y position
@light_x, @light_y, @initial_tx, @initial_ty = gets.split(" ").collect {|x| x.to_i}
thor_x = @initial_tx
thor_y = @initial_ty
# game loop
loop do
remaining_turns = gets.to_i # The remaining amount of turns Thor can move. Do not remove this line.
# Write an action using puts
# To debug: STDERR.puts "Debug messages..."
STDERR.puts @light_y, thor_y
direction = ''
if thor_y > @light_y
direction += 'N'
thor_y -= 1
elsif thor_y < @light_y
direction += "S"
thor_y += 1
end
if thor_x > @light_x
direction += "W"
thor_x -= 1
elsif thor_x < @light_x
direction += "E"
thor_x += 1
end
# A single line providing the move to be made: N NE E SE S SW W or NW
puts direction
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment