Skip to content

Instantly share code, notes, and snippets.

@weirddan455
Created December 23, 2021 22:05
Show Gist options
  • Save weirddan455/a9ca30b72b2edae973be3f4bd08487d6 to your computer and use it in GitHub Desktop.
Save weirddan455/a9ca30b72b2edae973be3f4bd08487d6 to your computer and use it in GitHub Desktop.
Advent of Code Day 22 Part 2
#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 Instruction
{
bool on;
int xMin;
int xMax;
int yMin;
int yMax;
int zMin;
int zMax;
} Instruction;
bool isNumber(char c)
{
if (c == '-')
{
return true;
}
return c > 47 && c < 58;
}
void insertionSort(int *d, int size)
{
for (int i = 1; i < size; i++)
{
for (int j = i; j > 0 && d[j - 1] > d[j]; j--)
{
int temp = d[j];
d[j] = d[j - 1];
d[j - 1] = temp;
}
}
}
int trimX(Instruction *ins, Instruction *trim, int x, int iSize)
{
int trimSize = 0;
for (int i = iSize - 1; i >= 0; i--)
{
if (x >= ins[i].xMin && x <= ins[i].xMax)
{
trim[trimSize++] = ins[i];
}
}
return trimSize;
}
int trimY(Instruction *ins, Instruction *trim, int y, int iSize)
{
int trimSize = 0;
for (int i = 0; i < iSize; i++)
{
if (y >= ins[i].yMin && y <= ins[i].yMax)
{
trim[trimSize++] = ins[i];
}
}
return trimSize;
}
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 %lu bytes. %lu expected.\n", bytesRead, fileInfo.st_size);
free(data);
return 1;
}
int numInstructions = 0;
for (ssize_t i = 0; i < bytesRead; i++)
{
if (data[i] == '\n')
{
numInstructions++;
}
}
Instruction *instructions = malloc(numInstructions * sizeof(Instruction));
if (instructions == NULL)
{
puts("malloc failed");
free(data);
return 1;
}
ssize_t dataIndex = 0;
for (int i = 0; i < numInstructions; i++)
{
if (data[dataIndex + 1] == 'n')
{
instructions[i].on = true;
dataIndex += 5;
}
else
{
instructions[i].on = false;
dataIndex += 6;
}
char string[16];
int stringIndex = 0;
while(isNumber(data[dataIndex]))
{
string[stringIndex++] = data[dataIndex++];
}
string[stringIndex] = 0;
instructions[i].xMin = atoi(string);
dataIndex += 2;
stringIndex = 0;
while(isNumber(data[dataIndex]))
{
string[stringIndex++] = data[dataIndex++];
}
string[stringIndex] = 0;
instructions[i].xMax = atoi(string);
dataIndex += 3;
stringIndex = 0;
while(isNumber(data[dataIndex]))
{
string[stringIndex++] = data[dataIndex++];
}
string[stringIndex] = 0;
instructions[i].yMin = atoi(string);
dataIndex += 2;
stringIndex = 0;
while(isNumber(data[dataIndex]))
{
string[stringIndex++] = data[dataIndex++];
}
string[stringIndex] = 0;
instructions[i].yMax = atoi(string);
dataIndex += 3;
stringIndex = 0;
while(isNumber(data[dataIndex]))
{
string[stringIndex++] = data[dataIndex++];
}
string[stringIndex] = 0;
instructions[i].zMin = atoi(string);
dataIndex += 2;
stringIndex = 0;
while(isNumber(data[dataIndex]))
{
string[stringIndex++] = data[dataIndex++];
}
string[stringIndex] = 0;
instructions[i].zMax = atoi(string);
dataIndex++;
}
free(data);
int dSize = numInstructions * 2;
int *xs = malloc(dSize * sizeof(int));
int *ys = malloc(dSize * sizeof(int));
int *zs = malloc(dSize * sizeof(int));
Instruction *xTrimmed = malloc(numInstructions * sizeof(Instruction));
Instruction *yTrimmed = malloc(numInstructions * sizeof(Instruction));
if (xs == NULL || ys == NULL || zs == NULL || xTrimmed == NULL || yTrimmed == NULL)
{
puts("malloc failed");
free(instructions);
return 1;
}
for (int i = 0, d = 0; i < numInstructions; i++, d += 2)
{
xs[d] = instructions[i].xMin;
xs[d + 1] = instructions[i].xMax + 1;
ys[d] = instructions[i].yMin;
ys[d + 1] = instructions[i].yMax + 1;
zs[d] = instructions[i].zMin;
zs[d + 1] = instructions[i].zMax + 1;
}
insertionSort(xs, dSize);
insertionSort(ys, dSize);
insertionSort(zs, dSize);
int64_t numOn = 0;
for (int x = 0; x < dSize - 1; x++)
{
int trimXSize = trimX(instructions, xTrimmed, xs[x], numInstructions);
for (int y = 0; y < dSize - 1; y++)
{
int trimYSize = trimY(xTrimmed, yTrimmed, ys[y], trimXSize);
for (int z = 0; z < dSize - 1; z++)
{
for (int i = 0; i < trimYSize; i++)
{
if (zs[z] >= yTrimmed[i].zMin && zs[z] <= yTrimmed[i].zMax)
{
if (yTrimmed[i].on)
{
int64_t width = xs[x + 1] - xs[x];
int64_t height = ys[y + 1] - ys[y];
int64_t depth = zs[z + 1] - zs[z];
numOn += (width * height * depth);
}
break;
}
}
}
}
}
printf("Number On: %ld\n", numOn);
free(instructions);
free(xs);
free(ys);
free(zs);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment