Puzzle two + gitignore
This commit is contained in:
parent
564b907aff
commit
df0e5d1bcf
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
main
|
||||||
1000
day1/puzzle2/input
Normal file
1000
day1/puzzle2/input
Normal file
File diff suppressed because it is too large
Load Diff
345
day1/puzzle2/main.c
Normal file
345
day1/puzzle2/main.c
Normal file
@ -0,0 +1,345 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int two_char_two_interger(int a, int b)
|
||||||
|
{
|
||||||
|
// int a is the 10's
|
||||||
|
// int b is the 1's
|
||||||
|
int num = (a * 10) + b;
|
||||||
|
// printf("Got number: %i\n", num);
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_one(char *str)
|
||||||
|
{
|
||||||
|
// printf("%c", ptr[j]);
|
||||||
|
char *str2;
|
||||||
|
str2 = (char *)malloc(3);
|
||||||
|
|
||||||
|
memcpy(str2, str, (size_t)3);
|
||||||
|
|
||||||
|
if (strcmp(str2, "one") == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_two(char *str)
|
||||||
|
{
|
||||||
|
// printf("%c", ptr[j]);
|
||||||
|
char *str2;
|
||||||
|
str2 = (char *)malloc(3);
|
||||||
|
|
||||||
|
memcpy(str2, str, (size_t)3);
|
||||||
|
|
||||||
|
if (strcmp(str2, "two") == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_three(char *str)
|
||||||
|
{
|
||||||
|
// printf("%c", ptr[j]);
|
||||||
|
char *str2;
|
||||||
|
str2 = (char *)malloc(5);
|
||||||
|
|
||||||
|
memcpy(str2, str, (size_t)5);
|
||||||
|
|
||||||
|
if (strcmp(str2, "three") == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_four(char *str)
|
||||||
|
{
|
||||||
|
// printf("%c", ptr[j]);
|
||||||
|
char *str2;
|
||||||
|
str2 = (char *)malloc(4);
|
||||||
|
|
||||||
|
memcpy(str2, str, (size_t)4);
|
||||||
|
|
||||||
|
if (strcmp(str2, "four") == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_five(char *str)
|
||||||
|
{
|
||||||
|
// printf("%c", ptr[j]);
|
||||||
|
char *str2;
|
||||||
|
str2 = (char *)malloc(4);
|
||||||
|
|
||||||
|
memcpy(str2, str, (size_t)4);
|
||||||
|
|
||||||
|
if (strcmp(str2, "five") == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_six(char *str)
|
||||||
|
{
|
||||||
|
// printf("%c", ptr[j]);
|
||||||
|
char *str2;
|
||||||
|
str2 = (char *)malloc(3);
|
||||||
|
|
||||||
|
memcpy(str2, str, (size_t)3);
|
||||||
|
|
||||||
|
if (strcmp(str2, "six") == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_seven(char *str)
|
||||||
|
{
|
||||||
|
// printf("%c", ptr[j]);
|
||||||
|
char *str2;
|
||||||
|
str2 = (char *)malloc(5);
|
||||||
|
|
||||||
|
memcpy(str2, str, (size_t)5);
|
||||||
|
|
||||||
|
if (strcmp(str2, "seven") == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_eight(char *str)
|
||||||
|
{
|
||||||
|
// printf("%c", ptr[j]);
|
||||||
|
char *str2;
|
||||||
|
str2 = (char *)malloc(5);
|
||||||
|
|
||||||
|
memcpy(str2, str, (size_t)5);
|
||||||
|
|
||||||
|
if (strcmp(str2, "eight") == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_nine(char *str)
|
||||||
|
{
|
||||||
|
// printf("%c", ptr[j]);
|
||||||
|
char *str2;
|
||||||
|
str2 = (char *)malloc(4);
|
||||||
|
|
||||||
|
memcpy(str2, str, (size_t)4);
|
||||||
|
|
||||||
|
if (strcmp(str2, "nine") == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const char *inputFile = "/home/ben/Documents/Projects/C/advent-of-code/day1/puzzle1/input";
|
||||||
|
int fd = open(inputFile, O_RDONLY);
|
||||||
|
|
||||||
|
if (fd < 0)
|
||||||
|
{
|
||||||
|
printf("\n\"%s \" could not open\n", inputFile);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
struct stat statbuf;
|
||||||
|
int err = fstat(fd, &statbuf);
|
||||||
|
if (err < 0)
|
||||||
|
{
|
||||||
|
printf("\n\"%s \" could not open\n", inputFile);
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ptr = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||||
|
if (ptr == MAP_FAILED)
|
||||||
|
{
|
||||||
|
printf("Mapping Failed\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int in_line = 0;
|
||||||
|
int first_num = -1;
|
||||||
|
int last_num;
|
||||||
|
char cur_char;
|
||||||
|
int total = 0;
|
||||||
|
// int numbers[1000];
|
||||||
|
int freePos = 0;
|
||||||
|
int lineOffset;
|
||||||
|
|
||||||
|
for (int i = 0; i < statbuf.st_size; i++)
|
||||||
|
{
|
||||||
|
if (in_line == 0)
|
||||||
|
{
|
||||||
|
in_line = 1;
|
||||||
|
for (i; i < __INT64_MAX__; i++)
|
||||||
|
{
|
||||||
|
lineOffset = i;
|
||||||
|
cur_char = ptr[i];
|
||||||
|
|
||||||
|
switch (cur_char)
|
||||||
|
{
|
||||||
|
case 'o': // one
|
||||||
|
if (is_one(&ptr[i]) == 0)
|
||||||
|
{
|
||||||
|
if (first_num == -1)
|
||||||
|
{
|
||||||
|
first_num = 1;
|
||||||
|
}
|
||||||
|
last_num = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 't': // two, three
|
||||||
|
if (is_two(&ptr[i]) == 1)
|
||||||
|
{
|
||||||
|
if (is_three(&ptr[i]) == 0)
|
||||||
|
{
|
||||||
|
if (first_num == -1)
|
||||||
|
{
|
||||||
|
first_num = 3;
|
||||||
|
}
|
||||||
|
last_num = 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (first_num == -1)
|
||||||
|
{
|
||||||
|
first_num = 2;
|
||||||
|
}
|
||||||
|
last_num = 2;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'f': // four, five
|
||||||
|
if (is_four(&ptr[i]) == 1)
|
||||||
|
{
|
||||||
|
if (is_five(&ptr[i]) == 0)
|
||||||
|
{
|
||||||
|
if (first_num == -1)
|
||||||
|
{
|
||||||
|
first_num = 5;
|
||||||
|
}
|
||||||
|
last_num = 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (first_num == -1)
|
||||||
|
{
|
||||||
|
first_num = 4;
|
||||||
|
}
|
||||||
|
last_num = 4;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 's': // six, seven
|
||||||
|
if (is_six(&ptr[i]) == 1)
|
||||||
|
{
|
||||||
|
if (is_seven(&ptr[i]) == 0)
|
||||||
|
{
|
||||||
|
if (first_num == -1)
|
||||||
|
{
|
||||||
|
first_num = 7;
|
||||||
|
}
|
||||||
|
last_num = 7;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (first_num == -1)
|
||||||
|
{
|
||||||
|
first_num = 6;
|
||||||
|
}
|
||||||
|
last_num = 6;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'e': // eight
|
||||||
|
if (is_eight(&ptr[i]) == 0)
|
||||||
|
{
|
||||||
|
if (first_num == -1)
|
||||||
|
{
|
||||||
|
first_num = 8;
|
||||||
|
}
|
||||||
|
last_num = 8;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'n': // nine
|
||||||
|
if (is_nine(&ptr[i]) == 0)
|
||||||
|
{
|
||||||
|
if (first_num == -1)
|
||||||
|
{
|
||||||
|
first_num = 9;
|
||||||
|
}
|
||||||
|
last_num = 9;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if isdigit (cur_char)
|
||||||
|
{
|
||||||
|
if (first_num == -1)
|
||||||
|
{
|
||||||
|
// We have not set a first number yet
|
||||||
|
first_num = (int)cur_char - '0';
|
||||||
|
last_num = (int)cur_char - '0';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
last_num = (int)cur_char - '0';
|
||||||
|
}
|
||||||
|
if (cur_char == 0x0A)
|
||||||
|
{
|
||||||
|
if (last_num != -1)
|
||||||
|
{
|
||||||
|
// printf("First num = %i\t", first_num);
|
||||||
|
// printf("Last num = %i\n", last_num);
|
||||||
|
total += two_char_two_interger(first_num, last_num);
|
||||||
|
}
|
||||||
|
first_num = -1;
|
||||||
|
last_num = -1;
|
||||||
|
in_line = 0;
|
||||||
|
// printf("Line break\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// printf("%X\n", ptr[i]);
|
||||||
|
}
|
||||||
|
printf("Total is: %i\n", total);
|
||||||
|
// ssize_t n = write(1, ptr, statbuf.st_size);
|
||||||
|
// if (n != statbuf.st_size)
|
||||||
|
// {
|
||||||
|
// printf("Write Failed");
|
||||||
|
// }
|
||||||
|
|
||||||
|
err = munmap(ptr, statbuf.st_size);
|
||||||
|
|
||||||
|
if (err != 0)
|
||||||
|
{
|
||||||
|
printf("Unmapping Failed\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user