Last active
June 11, 2018 11:41
-
-
Save martin-denizet/8b045d12e8d8eb9099460798c9545d1b to your computer and use it in GitHub Desktop.
Codingame: The is no Spoon - Episode 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import math | |
grid=[] | |
class Node(object): | |
def __init__(self,x,y): | |
self.x=x | |
self.y=y | |
def right_n(self): | |
right=list(filter(lambda n: n.x>self.x and n.y==self.y, grid)) | |
return right[0].to_s() if right else '-1 -1' | |
def bot_n(self): | |
bot=list(filter(lambda n: n.x==self.x and n.y>self.y, grid)) | |
return bot[0].to_s() if bot else '-1 -1' | |
def to_s(self): | |
return "{} {}".format(self.x,self.y) | |
# Don't let the machines win. You are humanity's last hope... | |
width = int(input()) # the number of cells on the X axis | |
height = int(input()) # the number of cells on the Y axis | |
for y in range(height): | |
line=input() | |
print("Input line: {}".format(line), file=sys.stderr) | |
for x, char in enumerate(list(line)): # width characters, each either 0 or . | |
if char=='0': | |
# Only create a node if the character is a 0 | |
grid.append(Node(x,y)) | |
for node in grid: | |
print("Processing node {}".format(node.to_s()), file=sys.stderr) | |
# Three coordinates: a node, its right neighbor, its bottom neighbor | |
print("{} {} {}".format(node.to_s(), node.right_n(), node.bot_n())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
grid=[] | |
height.times do | |
grid << gets.chomp.chars.collect{|c|c=='0'} # Make the line an array of booleans | |
end | |
grid.each_with_index do |line,row_index| | |
line.each_with_index do |cell,col_index| | |
next unless cell == true # Not a node, nothing to output | |
right_n=[-1,-1] | |
bot_n=[-1,-1] | |
# Scan right for node | |
(col_index+1..line.size-1).each do |index_right| | |
if line[index_right] == true | |
right_n=[index_right,row_index] | |
break | |
end | |
end | |
# Scan down for node | |
(row_index+1..grid.size-1).each do |index_bot| | |
if grid[index_bot][col_index] == true | |
bot_n=[col_index,index_bot] | |
break | |
end | |
end | |
puts "#{col_index} #{row_index} #{right_n.join(' ')} #{bot_n.join(' ')}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment