Compare commits

..

No commits in common. "94f7a90a82a36c43cec6290b90553b3d9a850eb1" and "bcc674bdf1a18ffb44c56a95e2ff76e9590b4829" have entirely different histories.

4 changed files with 2 additions and 129 deletions

2
.gitignore vendored
View file

@ -7,6 +7,4 @@
# Unignore all dirs
!*/
!Makefile
*.o

View file

@ -39,8 +39,8 @@ int main(void) {
}
for (size_t i = 0; i < lineCount - 1; i++) {
size_t leftMin = i;
size_t rightMin = i;
int leftMin = i;
int rightMin = i;
for (size_t j = i + 1; j < lineCount; j++) {
if (left[j] < left[leftMin]) {

View file

@ -1,87 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
unsigned int hash_int(unsigned int key) {
/* Robert Jenkins' 32 bit Mix Function */
key += (key << 12);
key ^= (key >> 22);
key += (key << 4);
key ^= (key >> 9);
key += (key << 10);
key ^= (key >> 2);
key += (key << 7);
key ^= (key >> 12);
/* Knuth's Multiplicative Method */
key = (key >> 3) * 2654435761;
return key;
}
#define TABLE_SIZE 16384
#define BUFFER_SIZE 1024
int main(void) {
FILE *fp = fopen("input", "r");
size_t lineCount = 0;
char buffer[BUFFER_SIZE] = {0};
// get number of lines
size_t read = 0;
while ((read = fread(&buffer, sizeof(char), BUFFER_SIZE, fp))) {
for (size_t i = 0; i < read; i++) {
if (buffer[i] == '\n') {
lineCount++;
}
}
}
rewind(fp);
// get data from file
unsigned int *left = malloc((lineCount * 2 + 1) * sizeof(int));
// haha pointer arithmetic go brrr
unsigned int *right = left + lineCount;
for (size_t i = 0; i < lineCount; i++) {
unsigned int leftNum, rightNum;
int numsGot = fscanf(fp, "%u %u\n", &leftNum, &rightNum);
if (numsGot == 2) {
left[i] = leftNum;
right[i] = rightNum;
}
}
unsigned int hash_map[TABLE_SIZE] = {0};
unsigned sum = 0;
for (size_t i = 0; i < lineCount; i++) {
const unsigned int key = hash_int(left[i]) % TABLE_SIZE;
if (hash_map[key] == 0) {
unsigned int similarity_score = 0;
for (size_t j = 0; j < lineCount; j++) {
if (left[i] == right[j]) {
similarity_score++;
}
}
similarity_score *= left[i];
hash_map[key] = similarity_score;
}
sum += hash_map[key];
}
printf("%d\n", sum);
free(left);
fclose(fp);
return 0;
}

View file

@ -1,38 +0,0 @@
# select all cpp files, for future use
SRC := ${wildcard *.c}
BIN := $(SRC:.c=)
# -pedantic means using gnu extentions will throw an error
# this allows for better code portability between compilers
CFLAGS := -std=c17
CFLAGS_ERRORS := -Werror -Wall -Wextra -Wconversion -Wdouble-promotion -Wunreachable-code -Wshadow -Wpedantic -pedantic-errors -pedantic
# debug is enabled by default
# debug can be disabled with `make DEBUG=0`
DEBUG := 1
ifeq ($(DEBUG),1)
# -Og compiles with debug specific optimizations
# -g3 compiles with max debug information
CFLAGS += -Og -g3
else
# -O3 compiles with extreme optimizations for performance
# -flto compiles with link time optimizations (LTO), supposed to improve performance by ~5%
CFLAGS += -O3 -flto
# -s when run on the linker will strip out debug symbols for a smaller file size
LDFLAGS += -s -flto
endif
# I believe using c++ is better since not every installation has gcc
# this can always be overridden with `make CC=gcc` if needed
CC := cc
all: $(BIN)
.c:
$(CC) $(CFLAGS) $(CFLAGS_ERRORS) $(LDFLAGS) -o $@ $^
clean:
$(DEL) $(TARGET) $(OBJ)
.PHONY: clean