Initial commit
This commit is contained in:
commit
564b907aff
1000
day1/puzzle1/input
Normal file
1000
day1/puzzle1/input
Normal file
File diff suppressed because it is too large
Load Diff
BIN
day1/puzzle1/main
Executable file
BIN
day1/puzzle1/main
Executable file
Binary file not shown.
107
day1/puzzle1/main.c
Normal file
107
day1/puzzle1/main.c
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <ctype.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 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;
|
||||||
|
|
||||||
|
for (int i = 0; i < statbuf.st_size; i++)
|
||||||
|
{
|
||||||
|
if (in_line == 0)
|
||||||
|
{
|
||||||
|
in_line = 1;
|
||||||
|
for (i; i < __INT64_MAX__; i++)
|
||||||
|
{
|
||||||
|
cur_char = ptr[i];
|
||||||
|
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