Time for another casual little challenge.
For this one there are only 2 rules:
you must add numbers a and b together
you must NOT use the +-*/ ...
For further actions, you may consider blocking this person and/or reporting abuse
Compile time execution goes brrrrrr 😂
Here is the full code with other solutions: https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=3fd17ceb138f1e2c230987bd93d93f25
Can you explain the trick with size?
The memory size of the struct
_Addhere would be the size of_aand_bcombined, in other words for any typeTand length ofn,[T; n]has the size ofn * size_of::<T>(), and since hereTisu8and its size is just1byte; hence[u8; n]is justnbytes. Using this; the size of_Addwould be the size of[u8; A](A bytes) and the size of[u8; B](B bytes); will result in total isA+Bbytes.Yes but this is where I'm lost. Wouldn't be the returned value 2 in every case?
No, in the test case,
A = 40means[0u8; 40]that also means40 bytes, andB = 2which is[0u8; 2]that's 2 bytes, which is total of42bytes.Ah indeed, thanks! I misunderstood the syntax.
edit : that's a really smart solution btw, thanks for sharing
Rust gang lesssgooo
Nice, that's really interesting! I like how the compiler replaces the Iterator example with
a + blol 🤣Crazy!
(for anyone wonder what iterators, check out the rust playground link I've posted, there is other solution that uses iterators instead of const generics)
Thats a nice logical solution
I mean, it looks cuter without
iforreturn:Wise solution 😜
Crystal or Ruby
Here's a really nasty one:
Cheers!
PS: Never do this. Is disgusting and it doesn't even add float numbers
It kinda is nasty 🤣 I like it though
I spent an unfortunate amount of time on this...
Output:
This currently only works on unsigned integers. I don't feel like spending the time now to remember how two's complement works.
Though I guess I'll be a bit more pedantic with this one:
for i in 0..32smuggles in some integer addition. So here's the set theoretic implementation, which has—less reliance on plus signs:Output:
Z means zero, and S means increment. So this is zero incremented eight times.
Thank you for the challenge, I finally got into WebAssembly 🎉
Here's another, more serious, attempt (still in Ruby) :
Some explanations : in Ruby
0..10is called a range. It is really useful to make arbitrary loops or splice an array, for example. Simply, a range is a suite of values. The syntax I used here is0...10(note the three dots) which is an exclusive range : the suite of values goes from0to9. So the trick is to have a range going from-atoband excludingbbecause0is included in the suite.In python
Nice cheat :p
This is clearly cheating, but here we go with a Ruby solution :D
Hahahah true xD if we stick to the rules yup... but I think I'll pass this one. I guessed there would be some languages that had built-in methods like this! What's the language BTW?
Ruby and, by extension, Crystal.
Nice one!! I also thought of this solution!!! Not very scalable but cool nonetheless
js
Dangerous one:
A bit shorter (if you don't mind polluting namespace):