Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created July 15, 2013 08:24
Show Gist options
  • Save dtinth/5998350 to your computer and use it in GitHub Desktop.
Save dtinth/5998350 to your computer and use it in GitHub Desktop.
(Codejam 2013) One Statement Lawnmower in Ruby
require 'matrix' and gets.to_i.times { |i| puts "Case ##{i+1}: #{(m = Matrix[*Array.new(gets.split.map(&:to_i)[0]) { gets.split.map(&:to_i) }]).enum_for(:each_with_index).all? { |c,i,j| [m.row(i).max, m.column(j).max].min == c } ? 'YES' : 'NO'}" }
require 'matrix' and # require the matrix library
gets.to_i # read the number of testcases
.times { |i| # for each test case
puts "Case ##{i+1}: #{ # print the case number
(m = Matrix[* # create a matrix from arrays
Array.new(gets.split.map(&:to_i)[0]) { # read 2 numbers N,M. create array of size N
gets.split.map(&:to_i) # each row, read in numbers for each row
}
]).enum_for(:each_with_index) # check every cell
.all? { |c,i,j|
[
m.row(i).max, # look horizontally and take the max height
m.column(j).max # look vertically and take the max height
].min == c # the minimum of the two must equal to current height
} ? 'YES' : 'NO' # format the text
}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment