# Thread: Wonka Factory Password

1. This is fairly difficult, i feel that they meticulously made it tough lol. Some of the problems that arose seem intentional.

like a boss...

2. Yep, I linked the book from the Amazon website in my post which says the latest delivery is by Tuesday but I didn't order it from Amazon lol Found another website that was delivering within 24 hours and so placed the order on there . It's arrived now and I'm going to start reading it tonight. And really, I found it good just by looking at the reviews and Wiki, and I think it'll be a good adventure reading it. Thanks.

Also, thanks to everyone who participated in this discussion. Really nice ideas have emerged (the linear solution by Hodor!) out of this discussion and have opened different ways for me to think in while solving problems.

3. working on the formula...

4. Code:
`if (time > 86) passWord = (((time-86)*20)+1094);`

5. That would work only for a specific test case, not all possible cases. If this formula was meant for the original test sample, your equation is wrong. Test it out yourself

What was the sample test you used which led you to this formula?

6. It is correct. Perhaps you are using it wrong?

The initial input is stated in the problem.

7. For the first 86 you have to use your algorithm to get the correct passwords.
The pattern starts at 86... for any time over 86:
Code:
`passWord = (((time-86)*20)+1094)`
This is a "shortcut".

time: 1001 = 19394
time: 50000000000 = 999999999374

as far as correct goes, every password at time, and strings displayed in the problem are 100% accurate.

your equation is wrong

8. If the algorithm is working correctly it should look like this...

initial state: ....#..#.#..##......###...###....

TIME: PASSWORD STRING
001: 091 ....#...#....#.....#..#..#..#..................
002: 132 ....##..##...##....#..#..#..##................
003: 102 ...#.#...#..#.#....#..#..#...#................
004: 154 ....#.#..#...#.#...#..#..##..##...............
005: 115 .....#...##...#.#..#..#...#...#...............
006: 174 .....##.#.#....#...#..##..##..##..............
007: 126 ....#..###.#...##..#...#...#...#..............
008: 213 ....#....##.#.#.#..##..##..##..##.............
009: 138 ....##..#..#####....#...#...#...#.............
010: 213 ...#.#..#...#.##....##..##..##..##............
011: 136 ....#...##...#.#...#.#...#...#...#............
012: 218 ....##.#.#....#.#...#.#..##..##..##...........
013: 133 ...#..###.#....#.#...#....#...#...#...........
014: 235 ...#....##.#....#.#..##...##..##..##..........
015: 149 ...##..#..#.#....#....#..#.#...#...#..........
016: 226 ..#.#..#...#.#...##...#...#.#..##..##.........
017: 170 ...#...##...#.#.#.#...##...#....#...#.........
018: 280 ...##.#.#....#####.#.#.#...##...##..##........
019: 287 ..#..###.#..#.#.#######.#.#.#..#.#...#........
020: 325 ..#....##....#####...#######....#.#..##.......

verified by: http://www.hpcodewars.org/past/cw22/...ProblemSet.pdf

9. Oh yes, my bad. Sorry about that. I subtracted 84 instead of 86 which led me to think that the equation was wrong. Well done

10. Originally Posted by Zeus_
Well done
Thank You. Being polite goes a long way.

How i solved this problem...
Code:
```#include <stdio.h>
#include <string.h>

#define bit 4

char *buffer;
char sit = '.', stand = '#';
char *initPattern = "#..#.#..##......###...###";
double time = 0.0;
char *mask = "...##..#...#....#.#..#.##.##...#####.#.##.#####.#.##.#####..###.#####.";
int mask_bits[14], places[128];

char toMatch[1024];

int addDots = 0;

char new_string[1024], string_buffer[1024];

int starter = 0;
double currentIndex;

double setString(char *string) {
int counter = 0, last = 0;
double passValue = 0;
int isReading = 0, bit_count = 0;
memset(string_buffer,0,strlen(string_buffer));

while (counter < strlen(string)) {
if (string[counter] == '#') {
last = (counter+1);
if (starter == 0) {
starter = counter;
if (counter == 0) strcat(string_buffer,"...");
};
passValue = (passValue + (counter));
};
counter++;
addDots = (counter-last);
};

if ((bit-starter) > 0) {
if (currentIndex == 1) {
for (int i=0;i<(bit-starter);i++) {
strcat(string_buffer,".");
};
};
};
strcat(string_buffer,string);
return passValue;
};

void doDots() {
for (int i=0;i<(bit-addDots);i++) strcat(string_buffer,".");
};

float cull(char *string,int count) {
int kickValue = 0;
for (int x=0;x<5;x++) {
if (string[count-x] == '#') {
if (x == 0) kickValue += 1;
if (x == 1) kickValue += 2;
if (x == 2) kickValue += 4;
if (x == 3) kickValue += 8;
if (x == 4) kickValue += 16;
};
};
return kickValue;
};

void patternLock(char *string) {
int counter = 0, startRead = 0;
int readCount = 0;
memset(toMatch,0,sizeof(toMatch));
while (counter < strlen(string)) {
if (counter == 4) startRead = 1;
if (startRead == 1) {
toMatch[counter-starter] = cull(string,counter-1);
readCount += 1;
};
counter++;
};
};

void validMatches() {
int ii = 0, ix = 0;
for (int i=0;i<strlen(mask);i++) { ii++;
if (ii == 5) { ii = 0;
mask_bits[ix] = (int)cull(mask,i);
ix++;
};
};
};

double retrieve(char *string) {
double x = setString(string);
return x;
};

void incorpPlaces() {
memset(places,0,sizeof(places));
for (int b=0;b<sizeof(toMatch);b++) {
for (int c=0;c<(sizeof(mask_bits)/sizeof(int));c++) {
if ((mask_bits[c] == toMatch[b]) && (toMatch[b] != 0) ) {
places[b] = mask_bits[c];
};
};
};
};

void displayResult(int id) {
if (id < 100) printf("0");
if (id < 10) printf("0");
printf("%i: ",id);
int passCode = 0;
memset(new_string,0,strlen(new_string));
for (int i=0;i<(128);i++) {
if (places[i] != 0) {
strcat(new_string,"#");
passCode += i-4;
} else {
strcat(new_string,".");
};
};

printf("%i_",passCode);
printf("%s\n",new_string);
};

int main(int args, char *argv[]) {

int maxGen = 100;

validMatches();

currentIndex = 1;
retrieve(initPattern); doDots();
printf("\n000: %s\n",string_buffer);
patternLock(string_buffer);
incorpPlaces();
displayResult(currentIndex);

for (int lot=2;lot<=maxGen;lot++) {
currentIndex = lot;
retrieve(new_string);
patternLock(string_buffer);
incorpPlaces();
displayResult(currentIndex);
};

return 0;
};```

11. Originally Posted by Structure
Thank You. Being polite goes a long way.

How i solved this problem...
That only solves the simple where: a) the cycle length is 1; and b) the cycle repeats. It doesn't deal with input like

Code:
```initial state ".#."
patterns
"....#"
"#...."```
Which makes a much prettier dance:

But much more difficult to solve

Edit: I wish I still had my code now. Anyway, The Nature of Code section 7.2. Look familiar? Now, there's probably a way to work calculate generation t for this particular case, like it's possible for the ones that end up being linear or semi-linear (cycling) but then how many other special cases are there and what if we get a random (see section 7.5 Wolfram Classification) one? I'm just going with what I said originally and considering this solved for cases where the pattern repeats in cycles of length=1 (i.e. the password being linear and the state being a "class 1" Wolfram class) (Cellular automaton - Wikipedia)

12. Some interesting reading as well (second link). In the second link scroll down to look at the graphs and read the discussion. Also worth reading is the section called "how hard are the problems? which is right at the bottom of the second link

Wolfram Rule 30 Prizes
Announcing the Rule 30 Prizes—Stephen Wolfram Writings

13. Look familiar?
yep...
initial state ".#."
patterns
"....#"
"#...."
I had to alter the code a little bit:

interesting...

14. much more difficult to solve
understatement.

Popular pages Recent additions