The memory size of the struct _Add here would be the size of _a and _b combined, in other words for any type T and length of n, [T; n] has the size of n * size_of::<T>(), and since here T is u8 and its size is just 1 byte; hence [u8; n] is just n bytes. Using this; the size of _Add would be the size of [u8; A] (A bytes) and the size of [u8; B] (B bytes); will result in total is A+B bytes.
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?
Though I guess I'll be a bit more pedantic with this one: for i in 0..32 smuggles in some integer addition. So here's the set theoretic implementation, which has—less reliance on plus signs:
#[derive(Debug)]
enum Num {
S(Box<Num>),
Z,
}
fn s(n: Num) -> Num {
Num::S(Box::new(n))
}
fn z() -> Num {
Num::Z
}
fn set_add(a: Num, b: Num) -> Num {
let mut result = a;
let mut work = b;
loop {
match work {
Num::S(next) => {
result = s(result);
work = *next;
},
Num::Z => return result,
}
}
}
fn main() {
let a = s(s(s(z()))); // i.e., 3
let b = s(s(s(s(s(z()))))); // i.e., 5
println!("3 + 5 = {:?}", set_add(a, b));
}
Output:
3 + 5 = S(S(S(S(S(S(S(S(Z))))))))
Z means zero, and S means increment. So this is zero incremented eight times.
Here's another, more serious, attempt (still in Ruby) :
defadd(a,b)(-a...b).sizeend
Some explanations : in Ruby 0..10 is 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 is 0...10 (note the three dots) which is an exclusive range : the suite of values goes from 0 to 9. So the trick is to have a range going from -a to b and excluding b because 0 is included in the suite.
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
Thats a nice logical solution
Wise solution 😜
Crystal or Ruby
Thank you for the challenge, I finally got into WebAssembly 🎉
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.
In python
Nice cheat :p
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.
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.Nice one!! I also thought of this solution!!! Not very scalable but cool nonetheless
Dangerous one:
js
A bit shorter (if you don't mind polluting namespace):
It kinda is nasty 🤣 I like it though