solved day 3 in reasonable runtime
This commit is contained in:
parent
e1fe054f63
commit
76be0eb02a
2 changed files with 90 additions and 0 deletions
13
3_2/Makefile
Normal file
13
3_2/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
|
||||||
77
3_2/main.c
Normal file
77
3_2/main.c
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
#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>
|
||||||
|
|
||||||
|
char input[] =
|
||||||
|
{
|
||||||
|
#embed "input.txt"
|
||||||
|
,'\0' // null terminator
|
||||||
|
};
|
||||||
|
|
||||||
|
int width = 0;
|
||||||
|
int height = 0;
|
||||||
|
|
||||||
|
uint64_t power(uint64_t base, uint64_t exp) {
|
||||||
|
uint64_t i, result = 1;
|
||||||
|
for (i = 0; i < exp; i++)
|
||||||
|
result *= base;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]){
|
||||||
|
clock_t start, end;
|
||||||
|
start = clock();
|
||||||
|
|
||||||
|
while(input[width] != '\n'){
|
||||||
|
width++;
|
||||||
|
}
|
||||||
|
//printf("width: %d\n", width);
|
||||||
|
|
||||||
|
while(input[height*(width+1)+1] != 0){
|
||||||
|
height++;
|
||||||
|
}
|
||||||
|
uint64_t count = 0;
|
||||||
|
for(int i = 0; i < height; i++){
|
||||||
|
//printf("\nChecking bank %d\n", i);
|
||||||
|
uint64_t biggest[12];
|
||||||
|
int biggest_index = -1;
|
||||||
|
memset(biggest, 0, 12*sizeof(uint64_t));
|
||||||
|
|
||||||
|
for(int x = 0; x<12; x++){
|
||||||
|
//printf("\tSearching for digit %d\n", x);
|
||||||
|
for(int o = biggest_index+1; o < width-11+x; o++){
|
||||||
|
|
||||||
|
int num = input[i*(width+1)+o]-0x30;
|
||||||
|
//printf("\t\tChecking: %d, index %d\n", num, o);
|
||||||
|
if(num>biggest[x]){
|
||||||
|
//printf("\t\t\tNew Biggest: %d, index %d\n", num, o);
|
||||||
|
biggest[x] = num;
|
||||||
|
biggest_index = o;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//printf("count: %lu += %lu * %lu\n", count, biggest[x], power(10, 11-x));
|
||||||
|
count += biggest[x]*power(10, 11-x);
|
||||||
|
//printf("count: %lu \n", count);
|
||||||
|
}
|
||||||
|
//for(int x = 0; x<12; x++){
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
//printf(input);
|
||||||
|
//printf("count: %lu\n", count);
|
||||||
|
}
|
||||||
|
|
||||||
|
end = clock();
|
||||||
|
printf("count: %lu\n", count);
|
||||||
|
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