Skip to content

Instantly share code, notes, and snippets.

@MrKelpy
Last active July 12, 2022 10:44
Show Gist options
  • Save MrKelpy/817427607441b27b1c1954386e844eeb to your computer and use it in GitHub Desktop.
Save MrKelpy/817427607441b27b1c1954386e844eeb to your computer and use it in GitHub Desktop.
blah#1594's "Dorky" Challenge
/*
* RULES
*0) Store the number 16022236204009818131831320183.
*1) Calculate the last 6 digits of the number.
*2) Calculate the value of the last 16 bits of the number.
*3a) Generate a new random number > 1,000,000.
*3b) Take the last 6 digits of the random number, and the value represented by the last 16 bits of it as well. Add their sum to a cumulative score.
*3c) Repeat step 3 until program runs for 1 second.
*4) Report the total score.
*(1) and (2) must be correct (320183, 61047).
*Score should be at least 2,000,000,000,000.
*/
package com.mrkelpy.test;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class Main {
public static void main(String[] args) {
// Declare useful variables for later usage
BigInteger number = new BigInteger("16022236204009818131831320183");
BigInteger eval = new BigInteger("200000000000");
Random rand = new Random();
int million = (int) Math.pow(10, 6);
// Get the last digits of the number, aswell as the value for the last 16 bits
int lastDigits = number.mod(BigInteger.valueOf((million))).intValueExact();
int last16BitsValue = Integer.parseInt(
Integer.toBinaryString(number.intValue()).substring(Integer.toBinaryString(number.intValue()).length() - 16), 2);
// Create the score, and get a timestamp roughly 1 second into the future
BigInteger score = BigInteger.valueOf(last16BitsValue).add(BigInteger.valueOf(lastDigits));
Timestamp ts = new Timestamp(System.currentTimeMillis() + 1000);
// Repeat the step of essentially repeating what we did before, but with a random number
// with a lower bound of 1 million, for 1 second
while (ts.compareTo(Timestamp.from(Instant.now())) > 0) {
int randnum = rand.nextInt(million) + million;
int last16BitsOfRandValue = Integer.parseInt(
Integer.toBinaryString(randnum).substring(Integer.toBinaryString(randnum).length() - 16), 2);
int last6DigitsOfRand = randnum % (int) Math.pow(10, 6);
score = score.add(BigInteger.valueOf(last6DigitsOfRand + last16BitsOfRandValue));
}
// Print the score and whether the challenge was completed.
System.out.println(score);
if (score.compareTo(eval) > 0) System.out.println("passed!");
else System.out.println("failed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment