m
This commit is contained in:
7
2015_day_5/Makefile
Normal file
7
2015_day_5/Makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
compile:
|
||||
gcc day_5.c -Werror=switch -o day5 -march=native -Ofast -flto
|
||||
|
||||
run:
|
||||
./day5
|
||||
run2:
|
||||
./day4 2
|
||||
138
2015_day_5/day_5.c
Normal file
138
2015_day_5/day_5.c
Normal file
@@ -0,0 +1,138 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
FILE *fptr;
|
||||
|
||||
|
||||
struct __attribute__((packed)) {
|
||||
unsigned int w0 : 1;
|
||||
unsigned int w1 : 1;
|
||||
unsigned int w2 : 1;
|
||||
unsigned int w3 : 1;
|
||||
unsigned int w4 : 1;
|
||||
unsigned int f0 : 1;
|
||||
} bitfield;
|
||||
|
||||
|
||||
//RULES: Must match all 3
|
||||
//Contains 3 vowels: aeiou only
|
||||
//At least one letter appears twice in a row
|
||||
//It does not contain the strings ab, cd, pq, or xy
|
||||
int main(){
|
||||
|
||||
fptr = fopen("input", "r");
|
||||
if(fptr == NULL) {
|
||||
printf("Not able to open the file.");
|
||||
return 1;
|
||||
}
|
||||
long long int line_count = 0;
|
||||
long long int nice_strings_count = 0;
|
||||
long long int new_nice_strings_count = 0;
|
||||
long long int naughty_strings_count = 0;
|
||||
char buffer[266];
|
||||
int cnt_1 = 0;
|
||||
int cnt_2 = 0;
|
||||
int cnt_3 = 0;
|
||||
char compare_1[3];
|
||||
char compare_2[3];
|
||||
while (fgets(buffer, sizeof(buffer), fptr)) {
|
||||
line_count++;
|
||||
bitfield.w0 = 0;
|
||||
bitfield.w1 = 0;
|
||||
bitfield.w2 = 0;
|
||||
bitfield.w3 = 0;
|
||||
bitfield.w4 = 0;
|
||||
bitfield.f0 = 0;
|
||||
cnt_1 = 0;
|
||||
cnt_2 = 0;
|
||||
cnt_3 = 0;
|
||||
//loop 1 aeiou
|
||||
for(int i = 0; i<strlen(buffer); i++){
|
||||
if (buffer[i] == 'a' || buffer[i] == 'e' || buffer[i] == 'i' || buffer[i] == 'o' || buffer[i] == 'u'){
|
||||
cnt_1 = cnt_1 + 1;
|
||||
}
|
||||
if (cnt_1 == 3){
|
||||
bitfield.w0 = 1;
|
||||
printf("break 1\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
//loop 2: letter repeated
|
||||
for(int i = 1; i<strlen(buffer); i++){
|
||||
if (buffer[i] == buffer[i-1]){
|
||||
cnt_2 = cnt_2 + 1;
|
||||
}
|
||||
if (cnt_2 == 1){
|
||||
bitfield.w1 = 1;
|
||||
printf("break 2\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
//loop 3: does not contain ab,cd, pq or xy
|
||||
|
||||
for(int i = 0; i< strlen(buffer) - 1; i++){
|
||||
if ((buffer[i] == 'a' && buffer[i+1] == 'b') || (buffer[i] == 'c' && buffer[i+1] == 'd') || (buffer[i] == 'p' && buffer[i+1] == 'q') || (buffer[i] == 'x' && buffer[i+1] == 'y')){
|
||||
bitfield.w2 = 1;
|
||||
printf("break 3\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
//loop 4: For Part 2: needs to have 2 identical 2 character substrings (separated by at least 1 char)
|
||||
for(int i = 0; i< strlen(buffer) - 1 && bitfield.f0 == 0; i++){
|
||||
sprintf(compare_1,"%.2s", buffer + i);
|
||||
for(int j = 2 + i; j < strlen(buffer) - 1; j++){
|
||||
sprintf(compare_2,"%.2s", buffer + j);
|
||||
if(strcmp(compare_1,compare_2) == 0){
|
||||
bitfield.w3 = 1;
|
||||
printf("break 4\n");
|
||||
bitfield.f0 = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
bitfield.f0 = 0;
|
||||
|
||||
//It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
|
||||
//loop 5: for part 2: finding matching letters with exactly one letter in between
|
||||
for(int i = 0; i< strlen(buffer) - 2; i++){
|
||||
sprintf(compare_1,"%.1s", buffer + i);
|
||||
sprintf(compare_2,"%.1s", buffer + i + 2);
|
||||
if(strcmp(compare_1,compare_2) ==0){
|
||||
printf("break 5\n");
|
||||
bitfield.w4 = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
//Thing nice and naughty string counting for Part 1
|
||||
if (bitfield.w1 == 1 && bitfield.w0 == 1 && bitfield.w2 ==0){
|
||||
nice_strings_count = nice_strings_count + 1;
|
||||
}
|
||||
else {
|
||||
naughty_strings_count = naughty_strings_count + 1;
|
||||
}
|
||||
|
||||
//Counting part 2
|
||||
if (bitfield.w3 == 1 && bitfield.w4 == 1){
|
||||
new_nice_strings_count = new_nice_strings_count + 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
memset(buffer, 0, sizeof(buffer)); // clear the buffer to zero
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
printf("naughty lines: %lli\n",naughty_strings_count);
|
||||
printf("nice lines: %lli\n", nice_strings_count);
|
||||
printf("nice lines part 2: %lli\n",new_nice_strings_count);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user