Created
December 14, 2021 11:25
-
-
Save weirddan455/2c5d1a3ede352e0ce81bdefb18a0e4b8 to your computer and use it in GitHub Desktop.
Advent of Code Day 14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fcntl.h> | |
#include <unistd.h> | |
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/stat.h> | |
typedef struct Template | |
{ | |
char input[2]; | |
char output; | |
uint64_t occurences; | |
} Template; | |
int main() | |
{ | |
int fd = open("input", O_RDONLY); | |
if (fd == -1) | |
{ | |
perror("open"); | |
return 1; | |
} | |
struct stat fileInfo; | |
if (fstat(fd, &fileInfo) != 0) | |
{ | |
perror("fstat"); | |
close(fd); | |
return 1; | |
} | |
char *data = malloc(fileInfo.st_size); | |
if (data == NULL) | |
{ | |
puts("malloc failed"); | |
close(fd); | |
return 1; | |
} | |
ssize_t bytesRead = read(fd, data, fileInfo.st_size); | |
close(fd); | |
if (bytesRead == -1) | |
{ | |
perror("read"); | |
free(data); | |
return 1; | |
} | |
if (bytesRead != fileInfo.st_size) | |
{ | |
printf("Error: read %d bytes. %d expected.\n", bytesRead, fileInfo.st_size); | |
free(data); | |
return 1; | |
} | |
ssize_t dataIndex = 0; | |
while (data[dataIndex] != '\n') | |
{ | |
dataIndex++; | |
} | |
dataIndex += 2; | |
int numTemplates = 0; | |
for (ssize_t i = dataIndex; i < bytesRead; i++) | |
{ | |
if (data[i] == '\n') | |
{ | |
numTemplates++; | |
} | |
} | |
Template *templates = malloc(numTemplates * sizeof(Template)); | |
Template *newTemplates = malloc(numTemplates * sizeof(Template)); | |
if (templates == NULL || newTemplates == NULL) | |
{ | |
puts("malloc failed"); | |
free(data); | |
return 1; | |
} | |
uint64_t occurences[91]; | |
for (int i = 0; i < 91; i++) | |
{ | |
occurences[i] = 0; | |
} | |
for (int i = 0; i < numTemplates; i++) | |
{ | |
templates[i].input[0] = data[dataIndex++]; | |
templates[i].input[1] = data[dataIndex]; | |
dataIndex += 5; | |
templates[i].output = data[dataIndex]; | |
dataIndex += 2; | |
templates[i].occurences = 0; | |
} | |
dataIndex = 0; | |
while (data[dataIndex + 1] != '\n') | |
{ | |
occurences[data[dataIndex]]++; | |
for (int i = 0; i < numTemplates; i++) | |
{ | |
if (data[dataIndex] == templates[i].input[0] && data[dataIndex + 1] == templates[i].input[1]) | |
{ | |
templates[i].occurences++; | |
break; | |
} | |
} | |
dataIndex++; | |
} | |
occurences[data[dataIndex]]++; | |
free(data); | |
memcpy(newTemplates, templates, numTemplates * sizeof(Template)); | |
for (int i = 0; i < 40; i++) | |
{ | |
for (int t = 0; t < numTemplates; t++) | |
{ | |
newTemplates[t].occurences = 0; | |
} | |
for (int t = 0; t < numTemplates; t++) | |
{ | |
occurences[templates[t].output] += templates[t].occurences; | |
char str1[2]; | |
str1[0] = templates[t].input[0]; | |
str1[1] = templates[t].output; | |
char str2[2]; | |
str2[0] = templates[t].output; | |
str2[1] = templates[t].input[1]; | |
for (int j = 0; j < numTemplates; j++) | |
{ | |
if (newTemplates[j].input[0] == str1[0] && newTemplates[j].input[1] == str1[1]) | |
{ | |
newTemplates[j].occurences += templates[t].occurences; | |
} | |
if (newTemplates[j].input[0] == str2[0] && newTemplates[j].input[1] == str2[1]) | |
{ | |
newTemplates[j].occurences += templates[t].occurences; | |
} | |
} | |
} | |
Template *temp = templates; | |
templates = newTemplates; | |
newTemplates = temp; | |
} | |
free(templates); | |
free(newTemplates); | |
uint64_t min = UINT64_MAX; | |
uint64_t max = 0; | |
for (int i = 0; i < 91; i++) | |
{ | |
if (occurences[i] > max) | |
{ | |
max = occurences[i]; | |
} | |
if (occurences[i] < min && occurences[i] > 0) | |
{ | |
min = occurences[i]; | |
} | |
} | |
uint64_t answer = max - min; | |
printf("Answer: %lu\n", answer); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment