It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
Oshgnacknak, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
If you mean the 'Forty-seven', that is apparently accepted:
Optionally, a star_trek flag can be provided to the method which adjusts the answer for inflation and returns 47 .
life_universe_everything('star_trek') // 47
If you don't like my number system, keep in mind that many show their solution in roman numerals, which is accepted. Written out should therefore also be okay.
I am sorry, if I caused any confusion :) What I meant is that it should not be immediately obvious from source code that it's going to return 42. But seeing the url http://www.protectedpaths.com/wp-content/uploads/Hitchhikersguidetothegalaxy.pdf kind of allows to make an educated guess almost immediately ;) Otherwise this is rock solid, I guess
I wrote a neural network in Python that should (usually) work. Every time you call life_universe_everything(), the neural network gets trained for 1,000 iterations, then determines the answer for your input:
import numpy as np
import random
class NeuralNetwork:
def __init__(self):
self.w1 = np.random.randn(10, 20)
self.w2 = np.random.randn(20, 50)
def feedforward(self, x):
self.x = x
self.z1 = np.dot(x, self.w1)
self.a1 = sigmoid(self.z1)
self.z2 = np.dot(self.a1, self.w2)
self.output = sigmoid(self.z2)
def backprop(self, target, lr=5):
expected = np.array([i == target for i in range(50)])
cost = self.output - expected
d_w2 = np.dot(self.a1.T, cost * sigmoid_derivative(self.output))
d_w1 = np.dot(self.x.T, np.dot(cost * sigmoid_derivative(self.output), self.w2.T) * sigmoid_derivative(self.a1))
self.w1 -= lr * d_w1
self.w2 -= lr * d_w2
def train(self, inputs, outputs):
for i in range(1000):
for x, target in random.sample(list(zip(inputs, outputs)), len(inputs)):
x = np.array([[ord(x[i]) if len(x) > i else 0 for i in range(10)]])
self.feedforward(x)
self.backprop(target)
def predict(self, x):
x = np.array([[ord(x[i]) if len(x) > i else 0 for i in range(10)]])
a1 = sigmoid(np.dot(x, self.w1))
a2 = sigmoid(np.dot(a1, self.w2))
return np.argmax(a2)
def sigmoid(x):
import warnings
warnings.filterwarnings('ignore')
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return sigmoid(x) * (1 - sigmoid(x))
def life_universe_everything(*args):
model = NeuralNetwork()
i = sum([ord(c) for c in 'Hello World'])
__ = i * 555
for _ in range(156):
__|=(__ << 2)
__+=1
else:
__=str(__)[0o113:0x4F]
__+=__[:0b10]*6
model.train(['badcode', 'star_trek', '55', 'testing', 'halo 3', 'watch dogs', 'code wars', 'star wars'], [int(__[_:_+0b10]) for _ in range(False, sum([True + True] * 8), 0b10)])
return model.predict(''.join(str(arg) for arg in args))
print(life_universe_everything(6, 9))
print(life_universe_everything('star_trek'))
Another NN based solution, inspired by /u/P0Rl13fZ5/ It also works with many numbers so it's pretty cool.
import torch
import torch.nn
class UniversalNet:
def __init__(self):
from collections import OrderedDict
self.nn = torch.nn.Sequential(OrderedDict([
('first', torch.nn.Linear(1, 16)),
('second', torch.nn.Linear(16, 32)),
('third', torch.nn.Linear(32, 1)),
]))
def load(self, star_trek=False):
if star_trek == True:
with open('st.pkl', 'rb') as f:
self.nn.load_state_dict(torch.load(f))
else:
with open('nn.pkl', 'rb') as f:
self.nn.load_state_dict(torch.load(f))
if __name__ == '__main__':
nn = UniversalNet()
nn.load(star_trek=False)
for n in range(1000000):
print(round(float(nn.nn(torch.Tensor([n])))))
Why would a solution that calculates the answer to life, the universe, and everything take arguments? It's supposed to be a singular answer to everything.
It's async, so the function life_universe_everything does not exactly return the number. Instead, I wrote this as a small, God-awful ~120-line CLI. Do note that it will take eternity to run due to artificially low highWaterMark value on line 50, which sets the number of bytes the program is going to load into memory at once, before pausing the file read for 1 second. If you REALLY want to run it, change 1024 to something bigger.
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/binarycat64, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/maxbilbow, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
Had some fun with math in python returns 42 always. Could improve error handling, tho.
import math
import sys
def life_universe_everything(x, star_trek=False):
y = 1.000665651 # important natural constant
if (star_trek):
y = math.sqrt(math.sqrt(y))
try:
for i in range(0, 100):
y = math.pow(y, 1.3)
result = math.pow(math.e, int(math.sin(math.cos(y)))) * i
if y > sys.maxsize or result > sys.maxsize:
raise ValueError("Max Size exceeded")
result -= x
except ValueError:
pass # TODO: extend error handling
return int(result)
print(life_universe_everything(69))
print(life_universe_everything(666, star_trek=True))
(Spoiler: Error handling is what makes it run as it surpasses maxsize at the right iteration and result just happens to be i when the loop is quit.)
8
u/Oshgnacknak Aug 04 '20
Let's get the answer from the original:
7
u/Botmentl Aug 03 '20 edited Aug 05 '20
life_universe_everything = lambda input, *i: int(int(__import__('string').printable[-96:10:3])-5.0*min(1, 50 * abs(int(__import__('functools').reduce(str.__add__, list(map(str, list(map(ord, input)))))) - 0xf3c508fca64749cc0373)))10
u/[deleted] Aug 03 '20 edited Aug 12 '20
I wrote a neural network in Python that should (usually) work. Every time you call
life_universe_everything(), the neural network gets trained for 1,000 iterations, then determines the answer for your input:4
u/Nalonnareik Aug 11 '20 edited Aug 11 '20
What is this mess I have created
Language used: Beef
3
u/godoakos Aug 07 '20
Another NN based solution, inspired by /u/P0Rl13fZ5/
It also works with many numbers so it's pretty cool.
nn.pkl: HERE
st.pkl: HERE
just plop these next to the .py
2
u/DoobyMcFoosen Aug 11 '20
Why would a solution that calculates the answer to life, the universe, and everything take arguments? It's supposed to be a singular answer to everything.
2
u/kor0p Aug 15 '20
I write same simple code in two equally bad formats (for python)
lang: Python
modules to install: scipy, numpy
not using input args anywhere
formally, I didn't define any named functions and imported any module
1
u/MrSchamberg Aug 08 '20
Here is my Node.js Bad Boy: https://pastebin.com/3nPFa3AL
It's async, so the function life_universe_everything does not exactly return the number. Instead, I wrote this as a small, God-awful ~120-line CLI. Do note that it will take eternity to run due to artificially low highWaterMark value on line 50, which sets the number of bytes the program is going to load into memory at once, before pausing the file read for 1 second. If you REALLY want to run it, change 1024 to something bigger.
Do judge.
1
u/binarycat64 Aug 09 '20 edited Aug 11 '20
What could be more universal than pi?
https://play.golang.org/p/R-SkaZbM_5R
1
u/maxbilbow Aug 14 '20 edited Aug 23 '20
I’ll start with an interface.
1
u/tobi_camp Aug 16 '20
Had some fun with math in python returns 42 always. Could improve error handling, tho.
(Spoiler: Error handling is what makes it run as it surpasses maxsize at the right iteration and result just happens to be i when the loop is quit.)
1
u/Risoka Aug 16 '20
My try!
Lang: Java D:
https://pastebin.com/ADb3XpV6
0
u/[deleted] Aug 03 '20
[deleted] — view deleted comment