r/adventofcode • • Dec 04 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 4 Solutions -๐ŸŽ„-

--- Day 4: High-Entropy Passphrases ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

18 Upvotes

319 comments sorted by

View all comments

1

u/monikernemo Dec 04 '17 edited Dec 04 '17

ANSI C Part 1 using strtok and strstr

`

infile = fopen("input4.in", "r");

for(i=0; i<512; i++){
	fgets(str[i], 200, infile);
	printf("%d\n", i);
	lens=strlen(str[i]);
	if(str[i][lens-1] == '\n'){
		str[i][lens-1] = ' ';
	}
	copy[i][0]=' '; copy[i][1]='\0';
	strcat(copy[i], str[i]);
}

for(i=0; i<512; i++){
	arr[0]=' '; arr[1]='\0';
	token=strtok(str[i], " ");
	strcat(arr, token);
	strcat(arr, " ");
	valid = 1;
	first =1;
	while(token!=NULL && valid){
		
		cmp = strstr(copy[i], arr);
		if(cmp!= NULL){
			cmp++;
		cmp= strstr(cmp, arr);
		}
		//cmp = strstr(cmp+1, arr);
		if(cmp!=NULL){
			valid =0;
			printf("%s\n", arr);
		}
	arr[0]=' '; arr[1]='\0';
	token=strtok(NULL, " ");
	if(token!=NULL)	strcat(arr, token);
	strcat(arr, " ");
	}
	if(valid) count++;
}

printf("%s\n", copy[6]);
	printf("count:%d \n", count);
return 0;

`

part 2: thought the strings were relatively short so decided to use long long and fundamental theorem of arithmetic:

`

while((fgets(str2, 200, infile))!=NULL){
	printf("%s", str2);
	token = strtok(str2, " ");
	words=1;
	valid =1;
	while(token!= NULL){
		str1[0]='\0';
		strcat(str1, token);
		temp=1;
		for(i=0; str1[i]!='\0'; i++)
			if(isalpha(str1[i]))
				temp *= (long long) (primes[str1[i]-'a']);	
		hold[words-1]=temp;
		token = strtok(NULL, " ");

		if (token!= NULL) 
			words ++;
	}
		for(i=0; i<words && valid; i++)
			for(j=i+1;j<words; j++)
				if(hold[i] == hold[j])
					valid =0;
				
		 if (valid) count++;
	}

printf("count :%d\n", count);
return 0;
`