Compare commits
3 commits
bcc674bdf1
...
94f7a90a82
Author | SHA1 | Date | |
---|---|---|---|
94f7a90a82 | |||
075f0ca632 | |||
b8064e8309 |
4 changed files with 129 additions and 2 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -7,4 +7,6 @@
|
||||||
# Unignore all dirs
|
# Unignore all dirs
|
||||||
!*/
|
!*/
|
||||||
|
|
||||||
|
!Makefile
|
||||||
|
|
||||||
*.o
|
*.o
|
|
@ -39,8 +39,8 @@ int main(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < lineCount - 1; i++) {
|
for (size_t i = 0; i < lineCount - 1; i++) {
|
||||||
int leftMin = i;
|
size_t leftMin = i;
|
||||||
int rightMin = i;
|
size_t rightMin = i;
|
||||||
|
|
||||||
for (size_t j = i + 1; j < lineCount; j++) {
|
for (size_t j = i + 1; j < lineCount; j++) {
|
||||||
if (left[j] < left[leftMin]) {
|
if (left[j] < left[leftMin]) {
|
||||||
|
|
87
day 1/2.c
Normal file
87
day 1/2.c
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
#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;
|
||||||
|
}
|
38
day 1/Makefile
Normal file
38
day 1/Makefile
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
# 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
|
Loading…
Reference in a new issue