Page 1 of 1
Mod
Posted: Sun Nov 23, 2008 1:01 am
by konsi
I had never programmed anything in ASM or any of that kind, so the HVM Challenges are quite a new experience for me. I tried the whole evening to beat this challenge and ended up with a 50char braintwist, which did the trick on my local python-HVM, but fortunately the challenge just wouldnt accept my solution: too many instructions.
Thus i leaned back, started the whole thinking all over and came to this solution:
0<1<0<1</*-p!
It just does the trick by abusing crude integer arithmetics. How nice.
Posted: Sun Nov 23, 2008 1:41 am
by theStack
Hi konsi,
congratulations for having such a good start with writing HVM, I remember my solution was not that short and elegant. However it is quite straight-forward if you write down the calculation in the common infix-notation and convert it to reverse polish notation
You can really look forward to the HVM-XOR challenge... that took me quite a while and it was sometimes a real pain in the ass but nothing compares to the feeling when it works finally
Posted: Sun Nov 23, 2008 1:55 pm
by m!nus
i have about the same answer as konsi. it was pretty easy after the HVM was changed to not work with float values anymore.
my before try was to divide the value by the mod and then keep on subtracting 1 while counting until it was <1. the solution then would've been number - counter*mod, but i think it ran out of cycles.
theStack: can you please explain your answer further?
Posted: Sun Nov 23, 2008 6:52 pm
by gfoot
I never tried with non-rounding division, but you could binary search instead of repeatedly decrementing. e.g for n%m, a binary search over integers in [0,n] for x such that n-x*m is in [0,m-1].
Posted: Sun Nov 23, 2008 10:24 pm
by theStack
m!nus wrote:
theStack: can you please explain your answer further?
I guess you mean the part speaking about the Mod challenge?
Well, calculating modula in our common infix notation looks like that, assumed that / performs integer division:
mem[0] - ((mem[0] / mem[1]) * mem[1])
And converting that to postfix is just that what konsi did.
Or did you mean the part about calculating XOR?
Posted: Mon Nov 24, 2008 4:28 pm
by m!nus
Nope, that's what I meant.
Posted: Fri Dec 05, 2008 10:50 am
by lukas
my code:
0<0<1</1<*-p
Posted: Wed Mar 18, 2009 1:32 am
by megabreit
It's not smaller but (slightly) different:
0<0^1</1<*-p
This was far easier than "King Rat" to me. I start having nightmares when I see the next HVM challenges coming
Posted: Mon Jan 02, 2012 9:59 pm
by rmplpmpl
At least one straightforward (for me anyway) HVM challenge...