Page 2 of 2

Re: Google Code Jam

Posted: Mon Apr 09, 2018 11:10 pm
by HermanHiddema
bernds wrote:Which language were you using? Perhaps something turned out not quite as portable between systems as one would hope.
Ruby. My code was:

Code: Select all

tests = gets.to_i
1.upto(tests) do |t|
	area = gets.to_i
	side = [(area/3.0).ceil, 3].max
	rect = side.times.map { [0,0,0] }
	while true
		best = 1.upto(side-2).min_by do |c|
			(-1..1).map do |x|
				(-1..1).map do |y|
					rect[c+x][y]
				end.sum
			end.sum
		end
		puts [best+1, 2].join(' ')
		STDOUT.flush
		x, y = gets.chomp.split.map(&:to_i)
		break if (x == 0 && y == 0)
		return if (x == -1 && y == -1)
		rect[x-1][y-1] = 1
	end
end
I don't think I'm using any special features here which haven't been present in ruby since forever.

The code simple creates a 3*X rectangle large enough that it covers sufficient area for the given test, then it keeps finding the 3x3 subrectangle with the most empty cells and outputs the middle cell thereof until the rectangle is filled.

Re: Google Code Jam

Posted: Tue Apr 10, 2018 4:25 am
by bernds
HermanHiddema wrote:
bernds wrote:Which language were you using? Perhaps something turned out not quite as portable between systems as one would hope.
Ruby. My code was:
When I try to run it from the script, I get:

Your code exited early, in the middle of Case #1.
Your code finishes with exit status 1.
The stderr output of your code is:
herman3.ruby:11:in `block (3 levels) in <main>': undefined method `sum' for [0, 0, 0]:Array (NoMethodError)
from herman3.ruby:8:in `each'
from herman3.ruby:8:in `map'
from herman3.ruby:8:in `block (2 levels) in <main>'
from herman3.ruby:7:in `upto'
from herman3.ruby:7:in `each'
from herman3.ruby:7:in `min_by'
from herman3.ruby:7:in `block in <main>'
from herman3.ruby:2:in `upto'
from herman3.ruby:2:in `<main>'

Re: Google Code Jam

Posted: Tue Apr 10, 2018 5:04 am
by HermanHiddema
Damn. https://blog.bigbinary.com/2016/11/02/r ... e-sum.html

$ ruby -v
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]

But the contest is 2.3.3

I've been using sum since forever, because it's been in ActiveSupport since forever, so I never realized it wasn't bog standard :/

Re: Google Code Jam

Posted: Tue Apr 10, 2018 5:38 am
by bernds
HermanHiddema wrote:Damn. https://blog.bigbinary.com/2016/11/02/r ... e-sum.html

$ ruby -v
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]

But the contest is 2.3.3

I've been using sum since forever, because it's been in ActiveSupport since forever, so I never realized it wasn't bog standard :/
Sorry to hear that. I knew Python was incompatible with itself, but I didn't know anything about Ruby. I'd kind of like to make a case for using industrial strength languages like C rather than these newfangled shiny but fragile things. Why handicap yourself in a contest?

Re: Google Code Jam

Posted: Tue Apr 10, 2018 6:04 am
by dfan
Ruby is over 20 years old, and (speaking as someone who has written more C++ than any other language) I think that when it comes to overall fragility, including how easy it is to introduce subtle bugs, C and C++ are much worse than languages like Ruby and Python.

It is true that you have to be a little careful about making sure that your versions match, which is true for any language with a rich standard library. The current version of Ruby is actually 2.5.1, and the latest version of Ruby 2.3 is 2.3.7, so if you ask me Google is the one who dropped the ball here if they only support 2.3.3.

Re: Google Code Jam

Posted: Tue Apr 10, 2018 6:32 am
by Kirby
In general, I preferred the old format for google code jam. You get the input file, do what you want in your own environment, and get an output file. There are fewer surprises, and nobody has to worry about environment details.

Re: Google Code Jam

Posted: Tue Apr 10, 2018 6:37 am
by bernds
dfan wrote:Ruby is over 20 years old, and (speaking as someone who has written more C++ than any other language) I think that when it comes to overall fragility, including how easy it is to introduce subtle bugs, C and C++ are much worse than languages like Ruby and Python.

It is true that you have to be a little careful about making sure that your versions match, which is true for any language with a rich standard library. The current version of Ruby is actually 2.5.1, and the latest version of Ruby 2.3 is 2.3.7, so if you ask me Google is the one who dropped the ball here if they only support 2.3.3.
I was half joking, half serious. But I wouldn't recommend C++ (which C++?) either for this purpose. And I don't think C really runs significantly more risk for subtle bugs for trivial problems such as these - and I note that Herman also had a mystery issue with problem 1.

Re: Google Code Jam

Posted: Tue Apr 10, 2018 7:41 am
by HermanHiddema
bernds wrote:I note that Herman also had a mystery issue with problem 1.
On problem 1 I got solved for the small set, wrong answer for the large set, so the code ran but gave the wrong output. That must've been my mistake, not ruby versions.

Re: Google Code Jam

Posted: Tue Apr 10, 2018 8:01 am
by HermanHiddema
bernds wrote:Sorry to hear that. I knew Python was incompatible with itself, but I didn't know anything about Ruby. I'd kind of like to make a case for using industrial strength languages like C rather than these newfangled shiny but fragile things. Why handicap yourself in a contest?
I write code way faster in modern languages like ruby or python. In this round, there was plenty of time, but in the next ones you're time-limited and it becomes significant. Also, these languages have some powerful default options (such as unlimited size ints and powerful enumerable manipulation libraries) that I don't have handy in C.

Really I should've just paid closer attention to the contest conditions. Switching to ruby 2.3 takes all of two seconds on my system with chruby, I just wasn't sufficiently prepared for the new contest format. :)