Google Code Jam

All non-Go discussions should go here.
User avatar
HermanHiddema
Gosei
Posts: 2011
Joined: Tue Apr 20, 2010 10:08 am
Rank: Dutch 4D
GD Posts: 645
Universal go server handle: herminator
Location: Groningen, NL
Has thanked: 202 times
Been thanked: 1086 times

Re: Google Code Jam

Post 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.
bernds
Lives with ko
Posts: 259
Joined: Sun Apr 30, 2017 11:18 pm
Rank: 2d
GD Posts: 0
Has thanked: 46 times
Been thanked: 116 times

Re: Google Code Jam

Post 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>'
User avatar
HermanHiddema
Gosei
Posts: 2011
Joined: Tue Apr 20, 2010 10:08 am
Rank: Dutch 4D
GD Posts: 645
Universal go server handle: herminator
Location: Groningen, NL
Has thanked: 202 times
Been thanked: 1086 times

Re: Google Code Jam

Post 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 :/
bernds
Lives with ko
Posts: 259
Joined: Sun Apr 30, 2017 11:18 pm
Rank: 2d
GD Posts: 0
Has thanked: 46 times
Been thanked: 116 times

Re: Google Code Jam

Post 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?
dfan
Gosei
Posts: 1599
Joined: Wed Apr 21, 2010 8:49 am
Rank: AGA 2k Fox 3d
GD Posts: 61
KGS: dfan
Has thanked: 891 times
Been thanked: 534 times
Contact:

Re: Google Code Jam

Post 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.
Kirby
Honinbo
Posts: 9553
Joined: Wed Feb 24, 2010 6:04 pm
GD Posts: 0
KGS: Kirby
Tygem: 커비라고해
Has thanked: 1583 times
Been thanked: 1707 times

Re: Google Code Jam

Post 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.
be immersed
bernds
Lives with ko
Posts: 259
Joined: Sun Apr 30, 2017 11:18 pm
Rank: 2d
GD Posts: 0
Has thanked: 46 times
Been thanked: 116 times

Re: Google Code Jam

Post 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.
User avatar
HermanHiddema
Gosei
Posts: 2011
Joined: Tue Apr 20, 2010 10:08 am
Rank: Dutch 4D
GD Posts: 645
Universal go server handle: herminator
Location: Groningen, NL
Has thanked: 202 times
Been thanked: 1086 times

Re: Google Code Jam

Post 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.
User avatar
HermanHiddema
Gosei
Posts: 2011
Joined: Tue Apr 20, 2010 10:08 am
Rank: Dutch 4D
GD Posts: 645
Universal go server handle: herminator
Location: Groningen, NL
Has thanked: 202 times
Been thanked: 1086 times

Re: Google Code Jam

Post 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. :)
Post Reply