Jump to content

Recommended Posts

I am trying to write a script for a puzzle based on the user's IP address, but I just can't wrap my head around how to do it. Essentially, when the visitor loads the page two things happen:

(1) a PHP application returns an image to serve as a background for the formula, 

(2) implement the algorithm of generating the formula based on IP, that is displayed on the image.

The puzzle itself would be in the form of ABC.DEF.GHI.JKL where each letter would be associated with the corresponding digit in their IP addy. The user is trying to solve for partial map coordinates. So, let's say the correct answer is N 35 54.374 W 084 01.829 and the user's IP is 075.036.025.058. The formula returned and placed on the image would be N 35 54. (E) (B) (F - H)  W 084 01. (L) (H) (B + H)

The algorithm basically needs to get the set of available digits from the IP and check if it can build the formula digits from those. If not, there would be a fallback for a missing number; for example given the above answer and if the available digits couldn't make a 9 then it would be represented by something like (C + 4).

Whatever I use as the final correct answer will not change and be the same for all users; only the puzzle formula would differ for each user as it would be based on their IP at the moment.

Any help here would be greatly appreciated. Thanks!

Gary

Link to comment
https://forums.phpfreaks.com/topic/311662-formula-based-on-ip-puzzle/
Share on other sites

That was a bit hard to understand, so I'll rephrase it:

The user is supposed to figure out N 35° 54.374' W 84° 01.829'. They are given most of it: N 35° 54.???' W 84° 01.???'. (Note that 1' is a little more than a mile, so you're giving them a lot of information here.)
Each of the six question marks is a single digit, and uses a simple expression based on the 12 digits in their IP address, which we'll say is ABC.DEF.GHI.JKL. Each expression is (a) a single letter if possible (digits be reused?), otherwise (b) two letters added or subtracted if possible, otherwise (c) one letter added or subtracted with a number.

If the user's IP address is 075.036.025.058 and the six missing digits are 3 7 4 8 2 9 then we can apply (a) to get 3=E, 7=B, 8=L, 2=H easily, but the 4 and 9 aren't available.
Next is to combine two digits using (b): 4 can't be formed through addition but it can through subtraction with 6-2=F-H or 7-3=B-E. 9 can be formed with 7+2=B+H or 6+3=F+E (as well as 2+7 and 3+6).
Don't need to invoke (c).

Obviously, you need to start by grabbing the IP address and splitting it into the digits you can work with, as well as figuring out the six digits of the coordinates you need to fill in.
Then you try to determine the formula for each of those six digits:

Using the (a) process is easy: if there's a digit in the IP address then pick it.

The (b) process is a couple loops:
1. Try addition. Loop from 1 to the digit-1 (or digit-1 to 1) - we can skip 0 because that would be digit+0 which (a) couldn't satisfy, and skip the digit itself for the same reason. If the number and (digit-number) is in the IP address then use them.
2. Try subtraction. Loop from 9 to the digit+1 (or digit+1 to 9). Again, if the number and (digit+number) exist in the IP address then use them. (There's actually a little more optimization you can do here.)

The (c) process is easy: pick a digit from the IP address, decide on either addition or subtraction (keeping in mind that one of those might put you out of the 0-9 bounds), and get the other number.

requinix, your rephrasing sums it up correctly. The abundance of information is ok; it is for a geocaching puzzle so the players will already know that the answer is within 2 miles of the listed coordinates. Their concern is narrowing it down to about 3 feet!

Your order of processes is perfect. The "d" process would then be to construct the formula and place it on an image, although the image would be strictly for effect and doesn't have to be a part. Unfortunately, I am just learning and have no clue how to make your processes happen.

Edited by gfogleman

Start writing code and see how it goes. If you have specific questions like how to get the IP address, then feel free to ask, but for your code you can just put in something temporary so that you can keep going.

$need = [3, 7, 4, 8, 2, 9]; // todo: get from the coordinates
$ip = "75.36.25.58"; // todo: get real address (and note actual addresses are not padded)

/* you'll need some code in here to break apart $ip... */

$digits = [
	"A" => // first (0)
	"B" => // second (7)
	"C" => // third (5)
	// etc
];

// returns a single letter or null if nothing
function choose_single_letter($needed, $digits) {
	// look for $needed in $digits

	if (/* $needed in $digits */) {
		return // the digit - if there's multiple matches then pick one (randomly?)
	} else {
		// nothing
		return null;
	}
}

// returns an array of [single letter, "+" or "-", single letter] or null if nothing
function choose_paired_letters($needed, $digits) {
	// addition loop
	for ($i = 1; $i <= $needed - 1; $i++) {
		if (/* $i in $digits and ($needed - $i) in digits */) {
			return [/* the digit for $i */, "+", /* the digit for $needed - $i */];
		}
	}

	// subtraction loop
	for ($i = 9; $i >= $needed + 1; $i--) {
		if (/* $i in $digits and ($needed + $i) in digits */) {
			return [/* the digit for $i */, "-", /* the digit for $needed + $i */];
		}
	}

	// nothing
	return null;
}

// returns an array of [single letter or number, "+" or "-", number or single letter]
function choose_any($needed, $digits) {
	// pick random digit in $digits

	if (/* $needed - digit >= 1 */) {
		return [/* digit */, "+", /* $needed - digit */];
	} else { // needed + digit <= 9
		return [/* $needed + digit */, "-", /* digit */];
	}
}

$exprs = [];
foreach ($need as $needed) {
	// call choose_single_letter, of that fails choose_paired_letters, or if that fails choose_any
	$exprs[] = // result
}

// now $exprs is an array where each entry is a thing that represents an expression:
// (a) a single letter to draw
// (b/c) an array in the form [operand, operator, operand] to draw

To get the result into the picture, that totally varies based on how you want to do that, but the best approach will very likely be to get the whole coordinates+expressions thing into a single string, kinda like

$string = "N 35° 54. (?) (?) (?) W 84° 01. (?) (?) (?)";

(substituting each question mark for the string representation of each expression that goes in there)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.