solved part 2, low-ish runtime
This commit is contained in:
parent
307f83d4e1
commit
2d6ef673f0
2 changed files with 119 additions and 0 deletions
106
6_2/main.c
Normal file
106
6_2/main.c
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
#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 parseNumVert(char* str, int len, int lines){
|
||||
int i = 0;
|
||||
while(str[i] == ' '){
|
||||
i+=len;
|
||||
}
|
||||
uint64_t out = 0;
|
||||
while((i/len)<lines && str[i] != ' '){
|
||||
//printf("%c", str[i]);
|
||||
out += str[i]-0x30;
|
||||
out *= 10;
|
||||
i+=len;
|
||||
}
|
||||
out /=10;
|
||||
//printf("\t%lu\n", out);
|
||||
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);
|
||||
}lines--;
|
||||
|
||||
|
||||
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", parseNumVert(input, width+1, lines));
|
||||
|
||||
|
||||
int parse_length = 1;
|
||||
uint64_t col_res = 0;;
|
||||
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 = 0; i<parse_length; i++){
|
||||
uint64_t num = parseNumVert(symbols-(lines*(width+1))+i, width+1, lines);
|
||||
//printf("parsed %lu\n", num);
|
||||
col_res *= num;
|
||||
}
|
||||
}else if(*symbols == '+'){
|
||||
col_res = 0;
|
||||
//printf("adding....\n");
|
||||
for(int i = 0; i<parse_length; i++){
|
||||
uint64_t num = parseNumVert(symbols-(lines*(width+1))+i, width+1, lines);
|
||||
//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