Life In 19x19
http://lifein19x19.com/

Google Code Jam
http://lifein19x19.com/viewtopic.php?f=8&t=15566
Page 1 of 2

Author:  HermanHiddema [ Tue Apr 03, 2018 5:27 am ]
Post subject:  Google Code Jam

Last year, a lot of L19'ers were interested and participated in the Google Code Jam. If anyone's interested this year, the qualifiers are coming up (April 6/7).

See: https://code.google.com/codejam/

I'll be participating :)

Author:  Bill Spight [ Tue Apr 03, 2018 7:06 am ]
Post subject:  Re: Google Code Jam

Good luck, Herman, and good luck to all! :D

Author:  Kirby [ Tue Apr 03, 2018 7:16 pm ]
Post subject:  Re: Google Code Jam

I'm going to try, though, I'll be in the airport for Korea this Friday when the qualification round starts. Given that it's for 24 hours, maybe I'll have the chance to get some points.

Author:  hyperpape [ Fri Apr 06, 2018 11:58 am ]
Post subject:  Re: Google Code Jam

After last year, I was a little annoyed with not solving more. I wanted to practice and be ready for this year. None of that happened.

Right now, I'm pretty excited about a separate project, and have been working on it every time I get a chance. I think I might try to do the Code Jam, but I can't decide what to spend time on.

Author:  Kirby [ Sat Apr 07, 2018 5:22 pm ]
Post subject:  Re: Google Code Jam

Well, in Korea now. A bit jet lagged, but saw that there is still time to work on the qualification round.

I did the first two problems. I can't seem to see how many you need to solve to advance to Round 1. Is it similar to last year?

Author:  Kirby [ Sat Apr 07, 2018 5:25 pm ]
Post subject:  Re: Google Code Jam

I see it now - 25 points. Cool.

Author:  bernds [ Sat Apr 07, 2018 5:46 pm ]
Post subject:  Re: Google Code Jam

Kirby wrote:
I see it now - 25 points. Cool.


And note the score displayed on the standings page is "optimistic", i.e. it assumes you get full points when the hidden results are revealed.

Author:  Kirby [ Sat Apr 07, 2018 6:04 pm ]
Post subject:  Re: Google Code Jam

Yeah, I get it. That part is the same as last year IIRC.

Author:  jeromie [ Sat Apr 07, 2018 6:39 pm ]
Post subject:  Re: Google Code Jam

I don't have time to participate this year, but I'm looking forward to the inevitable discussion that crop up around here! I learned a lot last year.

Author:  Kirby [ Sun Apr 08, 2018 1:02 am ]
Post subject:  Re: Google Code Jam

Well, that was quick - I'm out already. I only attempted the first two problems, and while they passed the small set on the first attempt, the large dataset gave TLE for both. I didn't do any sort of special optimization for either - just breadth first search for the first problem, and pretty much a straightforward implementation of the second.

Oh well. I guess I should have tried the other two problems, too (or thought about perf more, maybe).

Author:  bernds [ Sun Apr 08, 2018 4:00 am ]
Post subject:  Re: Google Code Jam

Kirby wrote:
Well, that was quick - I'm out already. I only attempted the first two problems, and while they passed the small set on the first attempt, the large dataset gave TLE for both. I didn't do any sort of special optimization for either - just breadth first search for the first problem, and pretty much a straightforward implementation of the second.

Oh well. I guess I should have tried the other two problems, too (or thought about perf more, maybe).


What do you mean by "breadth first search" in this case? The trick to the first one is that there is always one highest possible value swap. We can disregard all trailing Cs, then there is an irrelevant prefix followed by a number of S characters. Swapping the first of those S with the previous C will give you the maximum reduction. Example: SCSCSSS -> SCSSCSS.

In the second one, one has to not implement the quadractic bubble sort. The thing to realize here is that each swap leaves the middle element unchanged, so essentially it ends up sorting two arrays which are interleaved. So, put your input elements into two separate arrays as they come in, run quicksort on each, then look for mismatched elements when interleaving them again.

The one I didn't attempt was the two-rotations case for the cube. Played around with wxmaxima but couldn't really find an equation to give me the angle for a given area. I suppose a binary search could easily have found the right spot but at that point I felt I'd done enough.

Author:  Kirby [ Sun Apr 08, 2018 4:48 am ]
Post subject:  Re: Google Code Jam

bernds wrote:

What do you mean by "breadth first search" in this case? The trick to the first one is that there is always one highest possible value swap. We can disregard all trailing Cs, then there is an irrelevant prefix followed by a number of S characters. Swapping the first of those S with the previous C will give you the maximum reduction. Example: SCSCSSS -> SCSSCSS.


I didn't try to do any trick. For any given string, I swap each pair of consecutive characters to generate a bunch of new strings (each one representing a possible hack). I add these to a queue for the breadth first search I'm doing. Every time I pop something off of the queue, I check to see the damage. Also, whenever I add to the queue, I'm actually adding a pair to also indicate how many hacks I did to get that string. The unmodified string has 0 hacks, the first set of hacked strings all have 1 hack, the next iteration has 2 hacks, etc.

Anyway, I didn't try to think of any particular properties of swapping - I just generated all possibilities and explored them all. That's what I meant by BFS.

Quote:
In the second one, one has to not implement the quadractic bubble sort.


I implemented the quadratic bubble sort, literally taking the pseudocode from the problem description.

I guess I should have been trying to think of ways to optimize and make things fast, but I pretty much wrote things up literally to solve the problems, which I guess wasn't enough for the large datasets.

That being said, I still feel I've improved since last year, even though I didn't make it as far. Last year, I think you were able to tell when you got TLE, since it was manual upload of the output file, so I would have caught the fact that my implementations were too slow. And I wrote things up much faster than last year - in around 20 minutes for each problem, which is faster than last year.

So I'll mark it up as a success, even if I'm out so early this year.

Author:  Tryss [ Sun Apr 08, 2018 5:40 am ]
Post subject:  Re: Google Code Jam

That must have been... innefficient :shock:

My code for the two first problems (in C#) :

Problem 1 :
Code:
using System;
using System.Collections.Generic;
using System.Linq;

namespace Problem 1
{
    class Program
    {
        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());
            for (int t=1; t<=T; t++)
            {
                string[] s = Console.ReadLine().Split(' ');
                int result = Solve(int.Parse(s[0]), s[1]);

                Console.WriteLine("Case #" + t + ": " + ((result<0)?"IMPOSSIBLE":result.ToString()) );
            }
        }

        static int Solve(int D, string P)
        {
            List<int> instructions = new List<int>();
            int shot = 0;
            int charge = 1;
            for (int i = 0; i < P.Length; i++)
            {
                if (P[i] == 'C')
                {
                    charge *= 2;
                }
                else
                {
                    instructions.Add(charge);
                    shot++;
                }
            }

            if (shot > D)
                return -1;


            int result = 0;
            while ( instructions.Sum() > D )
            {
                int move = shot - 1;
                while( move > 0 && instructions[move] == instructions[move-1] )
                {
                    move--;
                }

                instructions[move] /= 2;
                result++;
            }

            return result;
        }
    }
}


Problem 2 :
Code:
using System;
using System.Collections.Generic;
using System.Linq;


namespace Problem_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());
            for (int t = 1; t <= T; t++)
            {
                Console.ReadLine();
                List<int> data = Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToList();
               
                int result = Solve(data);

                Console.WriteLine("Case #" + t + ": " + ((result < 0) ? "OK" : result.ToString()));
            }
        }


        static int Solve(List<int> data)
        {
            List<int> even = data.Where((c, i) => i % 2 == 0).ToList();
            List<int> odd = data.Where((c, i) => i % 2 != 0).ToList();

            even.Sort();
            odd.Sort();

            for( int i = 0; i<odd.Count; i++)
            {
                if (odd[i] < even[i])
                    return 2*i;

                if ((i+1)<even.Count && odd[i] > even[i + 1])
                    return 2*i+1;
            }

            return -1;
        }
    }
}



Note that in my first problem, I completly abstracted the sequence : I don't swap anything,

Author:  HermanHiddema [ Mon Apr 09, 2018 2:17 am ]
Post subject:  Re: Google Code Jam

Well, I didn't make it.

For problem 1 my code only solved the small set. I haven't looked at what went wrong with the large but I suspect something like an off by one error or perhaps a floating point inaccuracy from Math.log on large numbers.

For problem 2 I only wrote a quick brute force implementation which solves the small but not the large set.

For problem 3 I still have no idea what went wrong. My solution was quite efficient (equivalent to the more efficient solution mentioned in the analysis) and solved all test cases locally (using the provided test script, adjusted to give all sorts of test cases), but when I submitted it, it reported a failure with "Runtime Error" and I had no real way to debug. Very frustrating :mad:

I didn't start problem 4. After trying various ways to debug problem 3 I didn't feel like continuing, especially not on the math heavy problem 4, so I quit and went to do something more fun :)

Author:  bernds [ Mon Apr 09, 2018 5:23 am ]
Post subject:  Re: Google Code Jam

HermanHiddema wrote:
For problem 3 I still have no idea what went wrong. My solution was quite efficient (equivalent to the more efficient solution mentioned in the analysis) and solved all test cases locally (using the provided test script, adjusted to give all sorts of test cases), but when I submitted it, it reported a failure with "Runtime Error" and I had no real way to debug.
Which language were you using? Perhaps something turned out not quite as portable between systems as one would hope.

Author:  HermanHiddema [ Mon Apr 09, 2018 11:10 pm ]
Post subject:  Re: Google Code Jam

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:
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.

Author:  bernds [ Tue Apr 10, 2018 4:25 am ]
Post subject:  Re: Google Code Jam

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>'

Author:  HermanHiddema [ Tue Apr 10, 2018 5:04 am ]
Post subject:  Re: Google Code Jam

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 :/

Author:  bernds [ Tue Apr 10, 2018 5:38 am ]
Post subject:  Re: Google Code Jam

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?

Author:  dfan [ Tue Apr 10, 2018 6:04 am ]
Post subject:  Re: Google Code Jam

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.

Page 1 of 2 All times are UTC - 8 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/