Tag Archives: blogging

Back, and with hitboxes!

Hello there! After a short break, I’m finally getting back on track! This last month has been quite a ride, so many new experiences and, while not busy busy busy, been lazy lazy lazy. 🙂 Truth is, I’ve been resting or running errands when not working, and the little time I had at home I used to relax and rest a bit.

Can’t say it didn’t work out, after all, here I am once again! 😀

I took some time to also beat some S3&K with Super Tails (on the cellphone, touchscreen gaming is hard!) and Warcraft 2 (only did it before on the PlayStation, the PC version seems to be much nicer, although more rough), things I’ve wanted to do for quite a while!

While we’re at it, another game-related thing is my new commit on the MM3 repository! I’ve finally been able to test hitboxes (even the “debug” mode works now!) and it works great! It’s not a JAWS feature, but my implementation will do. I still need to adjust its size for the current animation (different shapes, different boxes), but that’s work for another commit.

Beside everything, now that I’m a full time Java developer, I’m learning more and more about its design patterns and how things go together on large systems. It’s been a wild ride, but I’m quickly getting used to. 🙂 Gives me the creeps to look at some of my old implementations, they desperately need refactoring! 😀

See ya!

Ancient Android root attempts on a chip RockChip tablet

Hello there! I’ve probably mentioned earlier that I’ve got two cheap tablets, and I got to root one of them. At least I was able to update it to 2.3 (I guess, don’t remember) with the Uberdroid distro image. Well, the other one has a RockChip chipset, and that’s quite harder to do, since z4root couldn’t trick it. I’m trying some other root methods, but for now, no success.

If after these new sideload attempts I’m still not successful… well, I’ll try some ADB methods with Windows applications. But that would be the last resort. Anyway, I’m playing a lot of music and taking care of pets/home when not at work, so, not much time to write and create new code. Let’s see how it goes. 🙂

Code Jam 2008 Round 1A, Problem C: Numbers

Hello again! This one is one hell of a tricky problem. No wonder it’s called “Numbers”:

In this problem, you have to find the last three digits before the decimal point for the number (3 + √5)n.

For example, when n = 5, (3 + √5)5 = 3935.73982… The answer is 935.

For n = 2, (3 + √5)2 = 27.4164079… The answer is 027.

Wow, that can’t be hard, eh? Indeed! It’s really just an expression, isn’t it? Of course it is! But once you try, you get to see something beautiful happening:

$ python
>>> import math
>>> 3+math.sqrt(5)
>>> (3+math.sqrt(5)) ** 18
>>> (3+math.sqrt(5)) ** 19
>>> (3+math.sqrt(5)) ** 20

If I was clear enough, you can see where I’m going. If not, the answers for n = 18, 19 and 20 should be 607, 263 and 151. But, it gets worse as the exponent grows:

>>> (3+math.sqrt(5)) ** 30
>>> (3+math.sqrt(5)) ** 40
>>> (3+math.sqrt(5)) ** 50

And those results should be 647, 551 and 247, respectively. As you can now clearly see, python can’t naturally handle those big floats, what a bummer! Well, the first thing to handle is the √5 value, since 2.23606797749979 does not give enough sampling to work with the needed precision. Looking a bit, I’ve found that python has a nice Decimal module that can deal with big numbers. So, let’s use it:

>>> from decimal import *
>>> Decimal(5).sqrt()

Hmm… not as much as I was expecting. Digging a little deeper, there’s a way to increase the precision, so things might get better:

>>> from decimal import *
>>> getcontext().prec = 256
>>> Decimal(5).sqrt()

Much better indeed! Let’s test the previous cases that way!

>>> (Decimal(3) + Decimal(5).sqrt()) ** Decimal(18)
>>> (Decimal(3) + Decimal(5).sqrt()) ** Decimal(19)
>>> (Decimal(3) + Decimal(5).sqrt()) ** Decimal(20)
>>> (Decimal(3) + Decimal(5).sqrt()) ** Decimal(30)
>>> (Decimal(3) + Decimal(5).sqrt()) ** Decimal(40)
>>> (Decimal(3) + Decimal(5).sqrt()) ** Decimal(50)

Whoa, now that’s improvement! Since the small input ranges from 2 to 30, we’re covered, yay! To the large input! The first case is 910062006… much bigger! But we have faith in our solution!

>>> (Decimal(3) + Decimal(5).sqrt()) ** Decimal(910062006)

Aaaaaand it fails miserably. You say increase the precision? Go!

>>> getcontext().prec = 1024
>>> (Decimal(3) + Decimal(5).sqrt()) ** Decimal(910062006)
>>> getcontext().prec = 1000000000
>>> (Decimal(3) + Decimal(5).sqrt()) ** Decimal(910062006)

It is still computing that last command… maybe it’ll finish before Christmas! 😀 So… nothing we can do about it. Not in a natural way at least. It turns out that Code Jam organizers knew about this (that’s the whole point, indeed) and my approach, even if it’s able to solve the small input, is not the correct one. From the contest analysis:

The key in solving the problem is a mathematical concept called conjugation. In our problem, we simply note that (3 – √5) is a nice conjugate for (3 + √5). Let us define

(1)     α := 3 + √5,   β := 3 – √5,   and Xn := αn + βn.

We first note that Xn is an integer. This can be proved by using the binomial expansion. If you write everything down you’ll notice that the irrational terms of the sums cancel each other out.


Another observation is that βn < 1, so Xn is actually the first integer greater than αn. Thus we may just focus on computing the last three digits of X.

Whoa, there’s a ton of math symbols there. What it is trying to explain is that there’s another way around, and we can write a simple algorithm to calculate only what we need. Taking that math apart, this suggested approach is quite interesting:

Solution C. [the periodicity of 3 digits]

For this problem, we have another approach based on the recurrence (7). Notice that we only need to focus on the last 3 digits of Xn, which only depends on the last 3 digits of the previous two terms. The numbers eventually become periodic as soon as we have (Xi, Xi+1) and (Xj, Xj+1) with the same last 3 digits, where i < j. It is clear that we will enter a cycle no later than 106 steps. In fact, for this problem, you can write some code and find out that the cycle has the size 100 and starts at the 3rd element in the sequence. So to solve the problem we can just brute force the results for the first 103 numbers and if n is bigger than 103 return the result computed for the number (n – 3) % 100 + 3.

Hmm… so that means that 103 restarts the cycle?

>>> getcontext().prec = 1024
>>> str((Decimal(3) + Decimal(5).sqrt()) ** Decimal(3))
>>> str((Decimal(3) + Decimal(5).sqrt()) ** Decimal(103))
>>> str((Decimal(3) + Decimal(5).sqrt()) ** Decimal(4))
>>> str((Decimal(3) + Decimal(5).sqrt()) ** Decimal(104))

Yay! To the git-pushed solution!

from decimal import *                            # Improved math

getcontext().prec = 1024                         # Adjust the precision
base = (Decimal(3) + Decimal(5).sqrt())          # Calculate the base

for case in range(1,int(raw_input())+1):         # For all test cases
  exp = (int(raw_input()) - 3) % 100 + 3         # Get the recurrent exponent

  num = str(base ** Decimal(exp))                # Calculate
  ans = num[:num.find('.')]                      # Get only the integer part

  print "Case #%d: %03d" % (case, int(ans[-3:])) # Zero-lead the last 3 digits

One thing that’s really interesting about this is that I actually solved this (of course, not from scratch) while posting! I was already OK with the idea of only solving the small input, but reading the analysis brought some new light into this problem. Blogging FTW! See ya!

Code Jam 2008 Round 1A, Problem A: Minimum Scalar Product

Hey there, since things are getting a little more calm, I can focus once more to solve some nice Code Jam problems. And here we go, from the first official tournament round: Problem A: Minimum Scalar Product. It says:

You are given two vectors v1=(x1,x2,…,xn) and v2=(y1,y2,…,yn). The scalar product of these vectors is a single number, calculated as x1y1+x2y2+…+xnyn.

Suppose you are allowed to permute the coordinates of each vector as you wish. Choose two permutations such that the scalar product of your two new vectors is the smallest possible, and output that minimum scalar product.

There’s a catch on the text, that made me quite confused. In case you didn’t notice, it’s that damn TWO PERMUTATIONS part. Despite getting the sample results right, when the bigger test cases came, I wasn’t able to get the smallest results right! Because, guess why, I was only doing two value permutations, because to me, the text says so!

Anyway, took a look into the results and saw that participants were simply not considering this limitation and outputting the minimum possible value anyway and to hell with it. If Google thinks this would be the correct approach, who am I to judge! 😀 Just sort both arrays, invert one of them, iterate calculating the MSP and you’re done! Python code, as usual:

for case in range(1,int(raw_input())+1): # For all test cases
  size = int(raw_input())                # Since we're informed, save array size
  x = map(int, raw_input().split(" "))   # Get all X parameters
  y = map(int, raw_input().split(" "))   # Get all Y parameters

  x.sort()                               # Sort both vectors
  y.sort()                               # And sort-reverse Y
  y.reverse()                            # So we can match the bigger/smaller values

  msp = 0
  for i in range(size):                  # For all the array's len
    msp += x[i]*y[i]                     # Sum "forward" on X and "backwards" on Y

  print "Case #%d: %d" % (case, msp)     # Print the sum

And that’s it! Quick and easy, despite the text hiccup.  Githubbin’ it. See you!

Some cleanup

Hey there, it’s hard to find time with Dexter and Breaking bad finales approaching! But they’ve already been dealt with for this week, so, more free time! 😀 Getting more tasks done, and not missing midnight, is quite an achievement for me.

Also, made a new commit from some of today and yesterday’s code on the new game, implemented a rough hitbox scheme, and still trying to debug it, works ok on bullets but not on enemies… perhaps it’s not a good approach to override a sprite’s rect() and I should implement my own collision functions, that look for hitboxes instead of them. Still, I can finally hit monsters and kill them (after the brief immunity period)!

I’m thinking about doing some cleanup tomorrow, on older posts, saw some code out of code tags, gonna check it out. I miss the CodeJam times, gotta do some more of those problems. Quite some work ahead!


Whoa… and we’re back!

Bah, the last couple of days almost made me die at HabitRPG, mostly because I must complete tasks before midnight (!) and I’ve got used to updating before going to sleep… and that was already late. I had something like 5HP left to avoid losing my beloved belongings, but tonight I made up and the experience gave me a new level (which restores all health), so I’m saved! 🙂 In a boring note, I could still have bought and used a potion, had the money to.

Been quite entertained with the new project, now I’m getting along with method encapsulation and developing resources as I need to. JAWS is awesome, but it lacks some features, such as hitboxes, and correctly being able to tell if a given element is partially inside the viewport … but I’m already implementing them, or didn’t look further into its code (the documentation is not as helpful as I need). JavaScript, in all its freedom, is a nice language but can be a pain sometimes too. Kudos to the guys that develop such great VMs to run all sorts of things, that must indeed be quite a challenge!

That’s it for today, just wanted to put some things out. Still gotta check Evernote everyday to get new blog subjects. I have so much to write about, but the lack of posts exists because I sometimes lack motivation to start the post… once I start writing, words flow nicely. 🙂