update
This commit is contained in:
328
app.cpp
328
app.cpp
@@ -1,118 +1,10 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#define ArrayCount(arr) (sizeof(arr) / sizeof((arr)[0]))
|
||||
|
||||
typedef int8_t int8;
|
||||
typedef int16_t int16;
|
||||
typedef int32_t int32;
|
||||
typedef int64_t int64;
|
||||
typedef uint8_t uint8;
|
||||
typedef uint16_t uint16;
|
||||
typedef uint32_t uint32;
|
||||
typedef uint64_t uint64;
|
||||
typedef uint8 byte;
|
||||
typedef uint16 b16;
|
||||
typedef uint32 b32;
|
||||
typedef uint64 b64;
|
||||
typedef float real32;
|
||||
typedef double real64;
|
||||
|
||||
struct string {
|
||||
uint8 *str;
|
||||
size_t len;
|
||||
};
|
||||
|
||||
struct StringNode {
|
||||
string *str;
|
||||
size_t start;
|
||||
size_t end;
|
||||
};
|
||||
|
||||
string asString(StringNode *node) {
|
||||
return {
|
||||
node->str->str + node->start,
|
||||
node->end - node->start,
|
||||
};
|
||||
}
|
||||
|
||||
string createStr(byte *str, size_t len) {
|
||||
string result;
|
||||
result.str = str;
|
||||
result.len = len;
|
||||
return result;
|
||||
}
|
||||
|
||||
#define strlit(lit) createStr((byte *)(lit), sizeof(lit) - 1)
|
||||
|
||||
#define Bytes(n) (n)
|
||||
#define Kilobytes(n) (n << 10)
|
||||
#define Megabytes(n) (n << 20)
|
||||
#define Gigabytes(n) (((uint64)n) << 30)
|
||||
#define Terabytes(n) (((uint64)n) << 40)
|
||||
|
||||
#define Thousand(n) ((n)*1000)
|
||||
#define Million(n) ((n)*1000000)
|
||||
#define Billion(n) ((n)*1000000000LL)
|
||||
|
||||
struct Arena {
|
||||
void *memory;
|
||||
size_t capacity;
|
||||
size_t head;
|
||||
};
|
||||
|
||||
void *pushSize(Arena *arena, size_t size) {
|
||||
if (arena->capacity - arena->head >= size) {
|
||||
arena->head += size;
|
||||
return (char *)arena->memory + arena->head + size;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define PushArray(arena, type, size) (type *)pushSize(arena, sizeof(type) * (size))
|
||||
#define PushStruct(arena, type, size) (type *)pushSize(arena, sizeof(type))
|
||||
|
||||
Arena createArena(size_t capacity) {
|
||||
Arena result = {};
|
||||
result.memory = mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
result.capacity = capacity;
|
||||
result.head = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t getFileSize(string file) {
|
||||
struct stat st;
|
||||
stat((char *)file.str, &st);
|
||||
return st.st_size;
|
||||
}
|
||||
|
||||
struct Buffer {
|
||||
size_t size;
|
||||
byte *data;
|
||||
};
|
||||
|
||||
Buffer readEntireFile(Arena *arena, string file) {
|
||||
FILE *input = fopen((char *)file.str, "r");
|
||||
size_t filesize = getFileSize(file);
|
||||
byte *readBuffer = PushArray(arena, byte, filesize);
|
||||
fread(readBuffer, sizeof(byte), filesize, input);
|
||||
fclose(input);
|
||||
return { filesize, readBuffer };
|
||||
}
|
||||
|
||||
int cmpint(const void *a, const void *b) {
|
||||
int *x = (int *)a;
|
||||
int *y = (int *)b;
|
||||
return (*x > *y) - (*x < *y);
|
||||
}
|
||||
#include <math.h>
|
||||
#include "core.hpp"
|
||||
|
||||
int day1() {
|
||||
Arena arena = createArena(Megabytes(16));
|
||||
|
||||
Buffer file = readEntireFile(&arena, strlit("./day-1-input"));
|
||||
list<char> file = readEntireFile(&arena, strlit("./day-1-input"));
|
||||
|
||||
int LIST_SIZE = 1000;
|
||||
int *firsts = PushArray(&arena, int, LIST_SIZE);
|
||||
@@ -123,7 +15,7 @@ int day1() {
|
||||
int line = 0;
|
||||
int *target = firsts;
|
||||
|
||||
for (int i = 0; i < file.size; i++) {
|
||||
for (int i = 0; i < file.length; i++) {
|
||||
char c = file.data[i];
|
||||
if (c == ' ') {
|
||||
continue;
|
||||
@@ -177,53 +69,213 @@ int day1() {
|
||||
|
||||
int day2_main() {
|
||||
Arena arena = createArena(Megabytes(16));
|
||||
Buffer file = readEntireFile(&arena, strlit("./day-2-input"));
|
||||
list<char> file = readEntireFile(&arena, strlit("./day-2-input"));
|
||||
|
||||
int safes = 0;
|
||||
int lines = 0;
|
||||
int *nums = PushArray(&arena, int, 100);
|
||||
int numLen = 1;
|
||||
int index = 0;
|
||||
while (index < file.size) {
|
||||
int linestart = index;
|
||||
int lineend = index;
|
||||
while (file.data[lineend] != '\n' && lineend < file.size) {
|
||||
int lines = 0;
|
||||
int fileReadIndex = 0;
|
||||
list<int> nums = PushList(&arena, int, 100);
|
||||
list<char> numStr = PushList(&arena, char, 16);
|
||||
list<int> modifiedNums = PushList(&arena, int, nums.capacity);
|
||||
while (fileReadIndex < file.length) {
|
||||
int linestart = fileReadIndex;
|
||||
int lineend = fileReadIndex;
|
||||
while (file.data[lineend] != '\n' && lineend < file.length) {
|
||||
lineend++;
|
||||
}
|
||||
lines++;
|
||||
char numStr[16] = {};
|
||||
int numStrLen = 1;
|
||||
numLen = 1;
|
||||
|
||||
zeroList(&nums);
|
||||
for (int j = linestart; j < lineend; j++) {
|
||||
if (file.data[j] != ' ') {
|
||||
numStr[numStrLen++ - 1] = file.data[j];
|
||||
} else {
|
||||
numStr[numStrLen - 1] = '\0';
|
||||
nums[numLen++ - 1] = atoi(numStr);
|
||||
numStrLen = 1;
|
||||
appendList(&numStr, (char)file.data[j]);
|
||||
}
|
||||
if (file.data[j] == ' ' || j == lineend - 1) {
|
||||
appendList(&numStr, '\0');
|
||||
appendList(&nums, atoi(numStr.data));
|
||||
zeroList(&numStr);
|
||||
}
|
||||
}
|
||||
bool increasing = nums[0] < nums[1];
|
||||
bool safe = true;
|
||||
for (int j = 0; j < numLen; j++) {
|
||||
int diff = increasing ? nums[j + 1] - nums[j] : nums[j] - nums[j + 1];
|
||||
if (increasing && (diff < 1 || diff > 3)) {
|
||||
safe = false;
|
||||
|
||||
int unsafes = 0;
|
||||
for (int i = 0; i < nums.length + 1; i++) {
|
||||
int skipIndex = i - 1;
|
||||
zeroList(&modifiedNums);
|
||||
for (int modifiedIndex = 0; modifiedIndex < nums.length; modifiedIndex++) {
|
||||
if (modifiedIndex == skipIndex) {
|
||||
continue;
|
||||
}
|
||||
appendList(&modifiedNums, nums.data[modifiedIndex]);
|
||||
}
|
||||
|
||||
bool increasing = modifiedNums.data[0] < modifiedNums.data[1];
|
||||
bool safe = true;
|
||||
for (int checkIndex = 0; checkIndex < modifiedNums.length - 1; checkIndex++) {
|
||||
int next = checkIndex + 1;
|
||||
int diff = increasing ? modifiedNums.data[next] - modifiedNums.data[checkIndex] : modifiedNums.data[checkIndex] - modifiedNums.data[next];
|
||||
if (diff < 1 || diff > 3) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!safe) {
|
||||
unsafes += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (safe) {
|
||||
|
||||
if (unsafes <= nums.length) {
|
||||
safes++;
|
||||
}
|
||||
index = lineend + 1;
|
||||
fileReadIndex = lineend + 1;
|
||||
}
|
||||
|
||||
printf("safes: %i", safes);
|
||||
printf("\n");
|
||||
printf("lines: %i", lines);
|
||||
printf("\n");
|
||||
printf("Arena: %zu, %zu", arena.head, arena.capacity);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool stringContains(string str, char c) {
|
||||
for (size_t i = 0; i < str.len; i++) {
|
||||
if (str.str[i] == c) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const char NUMERIC_CHARS[] = "1234567890";
|
||||
inline bool isNumeric(char c) {
|
||||
return stringContains(strlit(NUMERIC_CHARS), c);
|
||||
}
|
||||
|
||||
inline int parsePositiveInt(string str, size_t *lengthPointer) {
|
||||
size_t numEnd = 0;
|
||||
char currChar = str.str[numEnd];
|
||||
while (numEnd < str.len && isNumeric(currChar)) {
|
||||
currChar = str.str[++numEnd];
|
||||
*lengthPointer += 1;
|
||||
}
|
||||
*lengthPointer -= 1;
|
||||
if (numEnd > 0) {
|
||||
int result = 0;
|
||||
for (int i = 0; i < numEnd; i++) {
|
||||
result *= 10;
|
||||
result += (double)(str.str[i] - '0');
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int day3_main() {
|
||||
Arena arena = createArena(Megabytes(16));
|
||||
list<char> file = readEntireFile(&arena, strlit("./day-3-input"));
|
||||
|
||||
string mulBegin = strlit("mul(");
|
||||
string doIdent = strlit("do()");
|
||||
string dontIdent = strlit("don't()");
|
||||
|
||||
int result = 0;
|
||||
size_t c = 0;
|
||||
bool skip = false;
|
||||
while (c < file.length) {
|
||||
if (!skip) {
|
||||
if (file.data[c] == 'm') {
|
||||
string segment = strSlice(file.data, c, c+mulBegin.len);
|
||||
int first = -1;
|
||||
int second = -1;
|
||||
if (strEql(segment, mulBegin)) {
|
||||
c += segment.len;
|
||||
first = parsePositiveInt(strSlice(file.data, c, file.length - c), &c);
|
||||
if (file.data[c + 1] == ',') {
|
||||
c += 2;
|
||||
second = parsePositiveInt(strSlice(file.data, c, file.length - c), &c);
|
||||
if (second != -1 && file.data[c + 1] == ')') {
|
||||
c += 1;
|
||||
result += first * second;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (file.data[c] == 'd') {
|
||||
string segment = strSlice(file.data, c, c+dontIdent.len);
|
||||
if (strEql(segment, dontIdent)) {
|
||||
skip = true;
|
||||
c += dontIdent.len - 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (file.data[c] == 'd') {
|
||||
string segment = strSlice(file.data, c, c+doIdent.len);
|
||||
if (strEql(segment, doIdent)) {
|
||||
skip = false;
|
||||
c += doIdent.len - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
c++;
|
||||
}
|
||||
|
||||
printf("total: %i\n", result);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int day4_main() {
|
||||
Arena arena = createArena(Megabytes(16));
|
||||
list<char> file = readEntireFile(&arena, strlit("./day-4-input"));
|
||||
|
||||
int fileReadIndex = 0;
|
||||
int lines = 0;
|
||||
int lineLength = 0;
|
||||
while (fileReadIndex < file.length) {
|
||||
int linestart = fileReadIndex;
|
||||
int lineend = fileReadIndex;
|
||||
while (file.data[lineend] != '\n' && lineend < file.length) {
|
||||
lineend++;
|
||||
}
|
||||
if (lineLength == 0) {
|
||||
lineLength = lineend - linestart;
|
||||
} else if (lineend - linestart != lineLength) {
|
||||
fputs("Bad input. One or more lines aren't the same length as the first line.", stderr);
|
||||
return 1;
|
||||
}
|
||||
lines++;
|
||||
fileReadIndex += lineLength + 1;
|
||||
}
|
||||
|
||||
list<char> fileStripped = PushList(&arena, char, file.length - lines);
|
||||
for (size_t i = 0; i < file.length; i++) {
|
||||
if (file.data[i] != '\n') {
|
||||
appendList(&fileStripped, (char)file.data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
string xmas = strlit("XMAS");
|
||||
int c = 0;
|
||||
int count = 0;
|
||||
for (int c = 0; c < fileStripped.length; c++) {
|
||||
string segment = strSlice(fileStripped.data, c, c+xmas.len);
|
||||
const char *print = cstring(&arena, segment);
|
||||
printf("%s\n", print);
|
||||
if (strEql(segment, xmas)) {
|
||||
c += xmas.len;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("count: %i\n", count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
return day2_main();
|
||||
return day4_main();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user