solved day 6 part 1
This commit is contained in:
parent
be214cd92c
commit
0800c967f1
2 changed files with 118 additions and 0 deletions
13
6/Makefile
Normal file
13
6/Makefile
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
client: main.c
|
||||
gcc -o main main.c -O3 -Wall -Wextra -g -march=x86-64-v3
|
||||
|
||||
client-debug: main.c
|
||||
gcc -o main main.c -O1 -Wall -Wextra -g -fsanitize=address
|
||||
|
||||
client-debug-nosanitize: main.c
|
||||
gcc -o main main.c -O1 -Wall -Wextra -g
|
||||
|
||||
clean:
|
||||
rm main
|
||||
|
||||
.PHONY: clean
|
||||
105
6/main.c
Normal file
105
6/main.c
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
|
||||
char input[] =
|
||||
{
|
||||
#embed "input.txt"
|
||||
,'\0' // null terminator
|
||||
};
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
uint64_t result = 0;
|
||||
|
||||
|
||||
uint64_t parseNum(char* str){
|
||||
int i = 0;
|
||||
while(str[i] == ' '){
|
||||
i++;
|
||||
}
|
||||
uint64_t out = 0;
|
||||
while(str[i] != ' ' && str[i] != '\n'){
|
||||
//printf("%c", str[i]);
|
||||
out += str[i]-0x30;
|
||||
out *= 10;
|
||||
i++;
|
||||
}
|
||||
out /=10;
|
||||
return out;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
clock_t start, end;
|
||||
start = clock();
|
||||
|
||||
|
||||
while(input[width] != '\n'){
|
||||
width++;
|
||||
}
|
||||
|
||||
printf("width: %d\n", width);
|
||||
//parse ranges first
|
||||
int i = 0;
|
||||
int lines = 0;
|
||||
while(input[i+1] != 0){
|
||||
lines++;
|
||||
i+=(width+1);
|
||||
}
|
||||
|
||||
|
||||
char* symbols = input+(width+1)*(lines-1);
|
||||
printf("line count: %d\n", lines);
|
||||
printf("symbols:\n%s\n", symbols);
|
||||
|
||||
printf("parsing the first number: %lu\n", parseNum(input+8));
|
||||
|
||||
|
||||
int parse_length = 1;
|
||||
uint64_t col_res;
|
||||
while(*symbols != 0){
|
||||
while(symbols[parse_length] == ' ' || symbols[parse_length] == '\n'){
|
||||
parse_length++;
|
||||
}
|
||||
parse_length--;
|
||||
printf("parsing %d long numbers\n", parse_length);
|
||||
|
||||
if(*symbols == '*'){
|
||||
col_res = 1;
|
||||
printf("multiplying....\n");
|
||||
for(int i = 1; i<lines; i++){
|
||||
uint64_t num = parseNum(symbols-(i*(width+1)));
|
||||
printf("parsed %lu\n", num);
|
||||
col_res *= num;
|
||||
}
|
||||
}else if(*symbols == '+'){
|
||||
col_res = 0;
|
||||
printf("adding....\n");
|
||||
for(int i = 1; i<lines; i++){
|
||||
uint64_t num = parseNum(symbols-(i*(width+1)));
|
||||
printf("parsed %lu\n", num);
|
||||
col_res += num;
|
||||
}
|
||||
}
|
||||
printf("total: %lu\n", col_res);
|
||||
result += col_res;
|
||||
symbols += parse_length+1;
|
||||
parse_length = 1;
|
||||
}
|
||||
|
||||
end = clock();
|
||||
printf("count: %lu\n", result);
|
||||
clock_t ticks_taken = end - start;
|
||||
double time_taken = ((double)ticks_taken)/CLOCKS_PER_SEC;
|
||||
printf("time take: %f ticks\n", time_taken);
|
||||
printf("CLOCKS_PER_SEC: %ld\n", CLOCKS_PER_SEC);
|
||||
|
||||
|
||||
exit(1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue