Mod

Discussion of challenges you have already solved
Post Reply
konsi
Posts: 3
Joined: Wed Nov 19, 2008 3:51 pm

Mod

Post 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.
theStack
Posts: 72
Joined: Sun Nov 02, 2008 12:46 am

Post 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 :D

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 :wink:
User avatar
m!nus
Posts: 202
Joined: Sat Jul 28, 2007 6:49 pm
Location: Germany

Post 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?
gfoot
Posts: 269
Joined: Wed Sep 05, 2007 11:34 pm
Location: Brighton, UK

Post 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].
theStack
Posts: 72
Joined: Sun Nov 02, 2008 12:46 am

Post 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?
User avatar
m!nus
Posts: 202
Joined: Sat Jul 28, 2007 6:49 pm
Location: Germany

Post by m!nus »

Nope, that's what I meant.
lukas
Posts: 34
Joined: Wed Nov 26, 2008 1:53 pm
Location: Germany

Post by lukas »

my code:

0<0<1</1<*-p
megabreit
Posts: 141
Joined: Sat Jan 03, 2009 3:33 pm

Post 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 :shock:
rmplpmpl
Posts: 113
Joined: Sun Oct 26, 2008 10:38 am
Location: Germany

Post by rmplpmpl »

At least one straightforward (for me anyway) HVM challenge... :-)
Post Reply