What do I need to destroy the world?
JavaScript (ES6), 78 bytes
Takes input as 3 distinct variables (mass, radius, unit)
.
(m,r,u)=>m*m/r/[8987e9,56,6276e6,,4184e5,1e-4,360][parseInt(u,30)%7]/2498e11+u
Test cases
The following snippet runs all test cases in the provided spreadsheet.
let f=
(m,r,u)=>m*m/r/[8987e9,56,6276e6,,4184e5,1e-4,360][parseInt(u,30)%7]/2498e11+u
console.log(
[
'3.302e+23 2440000 1.789e+30 4.275e+17 2.850e+16 3.194e+24 4.969e+23 1.990e+13',
'4.869e+24 6052000 1.568e+32 3.748e+19 2.499e+18 2.800e+26 4.356e+25 1.745e+15',
'5.974e+24 6371000 2.242e+32 5.360e+19 3.573e+18 4.004e+26 6.229e+25 2.495e+15',
'6.419e+23 3390000 4.866e+30 1.163e+18 7.753e+16 8.689e+24 1.352e+24 5.414e+13',
'1.899e+27 69911000 2.065e+36 4.935e+23 3.290e+22 3.687e+30 5.736e+29 2.298e+19',
'5.685e+26 58232000 2.222e+35 5.310e+22 3.540e+21 3.968e+29 6.172e+28 2.472e+18',
'8.683e+25 25360000 1.190e+34 2.845e+21 1.896e+20 2.125e+28 3.306e+27 1.324e+17',
'1.024e+26 24620000 1.705e+34 4.075e+21 2.717e+20 3.045e+28 4.736e+27 1.897e+17',
'1.311e+22 1186000 5.801e+27 1.387e+15 9.244e+13 1.036e+22 1.611e+21 6.455e+10',
'1.989e+30 696300000 2.274e+41 5.436e+28 3.624e+27 4.062e+35 6.318e+34 2.531e+24',
'7.350e+22 1737000 1.245e+29 2.976e+16 1.984e+15 2.223e+23 3.458e+22 1.385e+12'
]
.map((l, n) => {
[m, r, ...res] = l.match(/\S+/g).map(Number);
return 'Test case #' + -~n + ' -> ' + (
['J', 'kilotons', 'nukes', 'twinkies', 'kWh', 'kg']
.every((u, i) => +(+f(m, r, u).split(u).join``).toExponential(3) === +res[i])
? 'OK' : 'FAIL'
)
})
)
How?
This is quite similar to @ColeraSu's C answer, with a JS-friendly hash function that works on the whole unit string.
Unit | Parsed as (*) | Base-30 -> Dec. | mod 7
-----------+---------------+-----------------+------
"J" | "j" | 19 | 5 (*):
"kilotons" | "kilotons" | 451052545318 | 4 Because we're parsing as Base-30,
"nukes" | "n" | 23 | 2 characters [u-z] (and anything
"twinkies" | "t" | 29 | 1 after them) are simply ignored.
"kWh" | "k" | 20 | 6
"kg" | "kg" | 616 | 0
Jelly, 78 bytes
²×⁽LÐ÷ȷ14÷⁴÷5÷⁵ị“¤Fðẏẏż“?A⁺ẏẏż“¬ŀż“8Ƙż“¡uþ³⁾ẉṿÆ“¡’¤;⁵ị“ÞṠlT“¡ṁæ-“;ØḲ“ȧ¹“ṫD“}»¤
Try it online!
Stuff to do with strings isn't really Jelly's type of challenge...
-7 bytes thanks to user202729 (thanks for helping me not get beaten by JavaScript :D)
C, 113 bytes
Hash the second character, using the trailing '\0'
of "J"
. Inline array in C is used.
Note we need to multiply the second M
last, or it will overflow to inf
.
f(m,r,c)float m,r;char*c;{printf("%e%s",m/r/(float[]){1e-4,360,6276e6,8987e9,56,4184e5}[c[1]%36%7]/2498e11*m,c);}
Try it online!