Skip to content

Instantly share code, notes, and snippets.

@weirddan455
Created December 25, 2021 08:56
Show Gist options
  • Save weirddan455/34554390fec92aadbd37a86ef8022d0d to your computer and use it in GitHub Desktop.
Save weirddan455/34554390fec92aadbd37a86ef8022d0d to your computer and use it in GitHub Desktop.
Advent of Code Day 25
#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>
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 width = 0;
int height = 0;
for (ssize_t i = 0; data[i] != '\n'; i++)
{
width++;
}
for (ssize_t i = 0; i < bytesRead; i++)
{
if (data[i] == '\n')
{
height++;
}
}
char *grid = malloc(width * height * sizeof(char) * 2);
if (grid == NULL)
{
puts("malloc failed");
free(data);
return 1;
}
char *readPointer = data;
char *writePointer = grid;
for (int y = 0; y < height; y++)
{
memcpy(writePointer, readPointer, width * sizeof(char));
readPointer += width + 1;
writePointer += width;
}
free(data);
char *grid2 = grid + (width * height);
memcpy(grid2, grid, width * height * sizeof(char));
readPointer = grid;
writePointer = grid2;
bool moved = true;
uint64_t steps = 0;
while(moved)
{
moved = false;
steps++;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
char *readFrom = &readPointer[(y * width) + x];
char *writeFrom = &writePointer[(y * width) + x];
char *readTo;
char *writeTo;
if (x < width - 1)
{
readTo = &readPointer[(y * width) + x + 1];
writeTo = &writePointer[(y * width) + x + 1];
}
else
{
readTo = &readPointer[y * width];
writeTo = &writePointer[y * width];
}
if (*readFrom == '>' && *readTo == '.')
{
*writeTo = '>';
*writeFrom = '.';
moved = true;
}
}
}
memcpy(readPointer, writePointer, width * height * sizeof(char));
char *temp = readPointer;
readPointer = writePointer;
writePointer = temp;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
char *readFrom = &readPointer[(y * width) + x];
char *writeFrom = &writePointer[(y * width) + x];
char *readTo;
char *writeTo;
if (y < height - 1)
{
readTo = &readPointer[((y + 1) * width) + x];
writeTo = &writePointer[((y + 1) * width) + x];
}
else
{
readTo = &readPointer[x];
writeTo = &writePointer[x];
}
if (*readFrom == 'v' && *readTo == '.')
{
*writeTo = 'v';
*writeFrom = '.';
moved = true;
}
}
}
memcpy(readPointer, writePointer, width * height * sizeof(char));
temp = readPointer;
readPointer = writePointer;
writePointer = temp;
}
printf("Steps: %lu\n", steps);
free(grid);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment