solved 9a and b, but needed another testcase to find the bug... ob1

This commit is contained in:
kirreen 2024-12-10 12:32:27 +01:00
parent 007f32ac40
commit 721b69ae20
12 changed files with 520 additions and 0 deletions

BIN
8/a.out Executable file

Binary file not shown.

50
8/input Normal file
View file

@ -0,0 +1,50 @@
...........g......................................
......e.g......s.............R...........I........
............g........................I............
..b......Q....s.....................P.............
.......e..T...................K...........P...F...
.....g................U.............4.............
.........b..........4..RU..................1.F....
....a.....Q..........b........R..U...............1
.S...T............s.............I.........f.......
...A....T...............................I.........
.....Qa............A.G...K...........P............
...........................G................1.....
...D...................................4.f........
..................................................
................k.......R....................t....
.........T.e..............K........u.......t......
....................A.............................
......S....a.............F...........KG...........
....D...h......k..................................
..D...............k............................4..
..............................................i...
.........S..................d.....................
......QU....S......s..d...G............i..........
...........d....9...F.h...E.......................
.................d....B...........................
...h........................H.......t.............
.........h.B..3.E.............H..r................
.......E......B......2...5.....H..................
.z...........9................................t...
.....9...D........................................
.....Z.......39..a................................
..........3.............r.........................
...............Er.............................7...
.........................J...k.r.q.......i.8.p....
.......................u...............H.p..q.....
..............................i.u6........p.......
....................................0.............
...............3..J....P...0......................
........................2j........................
...............................j2B0...............
................J..2...5.....6......p....8........
............y.........................7...........
..............5.........y...........6.............
................................j.................
.........................Y.J.....0................
.........................................y........
..................Z...uy...................q......
.......z.........Z............Y.6.............8...
z.........................Y..........7............
....................Z...5..Y......................

12
8/inputDemo Normal file
View file

@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

106
8/main.c Normal file
View file

@ -0,0 +1,106 @@
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<unistd.h>
struct antenna {
char type;
int xy[2];
struct antenna *next;
};
void add_antenna(struct antenna *head, char type, int *xy)
{
// traverse list until last member
while(head->next) head = head->next;
// allocate and store ptr
if (head->type) {
head->next = malloc(sizeof(struct antenna));
head = head->next;
}
head->type = type;
head->xy[0] = xy[0];
head->xy[1] = xy[1];
head->next = 0;
}
int main ()
{
struct antenna *list = malloc(sizeof(struct antenna));
list->type = '\0';
// figure out data dimensions
FILE *input = fopen("./input", "r");
int width = 0;
int height = 1;
while (fgetc(input) != '\n') width++;
for (char c = fgetc(input); c != EOF; c = fgetc(input)) {
if (c == '\n')
height++;
}
rewind(input);
printf("Dimensions: %d x %d\n", width, height);
// make 2d array
char **grid = malloc(width * sizeof(char*));
grid[0] = malloc(width * height);
for (int i = 1; i < width; i++) {
grid[i] = grid[0] + i * height;
}
// put the data in the array
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
grid[j][i] = fgetc(input);
if (grid[j][i] != '.') {
// store antenna in list
int xy[] = {j, i};
add_antenna(list, grid[j][i], xy);
printf("Antenna %c coords: %d/%d\n", grid[j][i], j, i);
}
}
// skip newline
fgetc(input);
}
fclose(input);
struct antenna *current = list;
struct antenna *other = list;
// for each antenna in list
while (current) {
// find delta to other of same frequency
while (other) {
if (current->type == other->type && current != other) {
int delta[2];
delta[0] = other->xy[0] - current->xy[0];
delta[1] = other->xy[1] - current->xy[1];
// apply # to other + delta
if (
(other->xy[0] + delta[0]) >= 0 &&
(other->xy[0] + delta[0]) < width &&
(other->xy[1] + delta[1]) >= 0 &&
(other->xy[1] + delta[1]) < height) {
grid[other->xy[0] + delta[0]][other->xy[1] + delta[1]] = '#';
}
}
other = other->next;
}
other = list;
current = current->next;
}
// iterate through array again and sum it
int sum_x = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (grid[j][i] == '#')
sum_x++;
printf("%c", grid[j][i]);
}
printf("\n");
}
printf("total antinodes: %d\n", sum_x);
}

121
8/mainB.c Normal file
View file

@ -0,0 +1,121 @@
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<unistd.h>
struct antenna {
char type;
int xy[2];
struct antenna *next;
};
void add_antenna(struct antenna *head, char type, int *xy)
{
// traverse list until last member
while(head->next) head = head->next;
// allocate and store ptr
if (head->type) {
head->next = malloc(sizeof(struct antenna));
head = head->next;
}
head->type = type;
head->xy[0] = xy[0];
head->xy[1] = xy[1];
head->next = 0;
}
int main ()
{
struct antenna *list = malloc(sizeof(struct antenna));
list->type = '\0';
// figure out data dimensions
FILE *input = fopen("./input", "r");
int width = 0;
int height = 1;
while (fgetc(input) != '\n') width++;
for (char c = fgetc(input); c != EOF; c = fgetc(input)) {
if (c == '\n')
height++;
}
rewind(input);
printf("Dimensions: %d x %d\n", width, height);
// make 2d array
char **grid = malloc(width * sizeof(char*));
grid[0] = malloc(width * height);
for (int i = 1; i < width; i++) {
grid[i] = grid[0] + i * height;
}
// put the data in the array
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
grid[j][i] = fgetc(input);
if (grid[j][i] != '.') {
// store antenna in list
int xy[] = {j, i};
add_antenna(list, grid[j][i], xy);
printf("Antenna %c coords: %d/%d\n", grid[j][i], j, i);
}
}
// skip newline
fgetc(input);
}
fclose(input);
struct antenna *current = list;
struct antenna *other = list;
// for each antenna in list
while (current) {
// find delta to other of same frequency
while (other) {
if (current->type == other->type && current != other) {
int delta[2];
delta[0] = other->xy[0] - current->xy[0];
delta[1] = other->xy[1] - current->xy[1];
// apply # to other + delta
int pos[2] = {other->xy[0], other->xy[1]};
while (
(pos[0] + delta[0]) >= 0 &&
(pos[0] + delta[0]) < width &&
(pos[1] + delta[1]) >= 0 &&
(pos[1] + delta[1]) < height) {
pos[0] = pos[0] + delta[0];
pos[1] = pos[1] + delta[1];
grid[pos[0]][pos[1]] = '#';
}
pos[0] = other->xy[0];
pos[1] = other->xy[1];
while (
(pos[0] - delta[0]) >= 0 &&
(pos[0] - delta[0]) < width &&
(pos[1] - delta[1]) >= 0 &&
(pos[1] - delta[1]) < height) {
pos[0] = pos[0] - delta[0];
pos[1] = pos[1] - delta[1];
grid[pos[0]][pos[1]] = '#';
}
}
other = other->next;
}
other = list;
current = current->next;
}
// iterate through array again and sum it
int sum_x = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (grid[j][i] == '#')
sum_x++;
printf("%c", grid[j][i]);
}
printf("\n");
}
printf("total antinodes: %d\n", sum_x);
}

BIN
9/a.out Executable file

Binary file not shown.

1
9/input Normal file

File diff suppressed because one or more lines are too long

1
9/input2 Normal file
View file

@ -0,0 +1 @@
12101

1
9/inputDemo Normal file
View file

@ -0,0 +1 @@
2333133121414131402

89
9/main.c Normal file
View file

@ -0,0 +1,89 @@
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
int main ()
{
// figure out data dimensions
FILE *input = fopen("./input", "r");
int width = 0;
bool flip = 1;
unsigned int dataSum = 0;
unsigned int dataID = 0;
int dataSlot = 0;
for (char c = fgetc(input); c != EOF && c != '\n'; c = fgetc(input)) {
dataSum += c - '0';
width++;
}
rewind(input);
printf("Length: %d\ndataSum: %u\n", width, dataSum);
unsigned int *memory = malloc(dataSum * sizeof(int));
// -1 represents empty memory
for (int i = 0; i < dataSum; i++) {
memory[i] = -1;
}
// time to fill the array
for (int i = 0; i < width; i++) {
char c = fgetc(input);
if (c == EOF) {
printf("breakbreakbreak!\n");
break;
}
// fan ska ju bara vara varannan som är data, resten free space
// flipflopflipflop ^
for (int j = 0; j < (c - '0'); j++) {
if (flip) {
memory[dataSlot] = dataID;
printf("%u", memory[dataSlot]);
} else {
memory[dataSlot] = -1;
printf(".");
}
dataSlot++;
}
if (flip)
dataID++;
flip ^= true;
}
fclose(input);
printf("\n");
// it's compacting time!
// start at end of array, save id, overwrite, put in first free spot
int lastWrite = 0;
for (int i = dataSum - 1; i >= 0; i--) {
if (memory[i] == -1)
continue;
if (lastWrite >= i)
goto exitDaLoop;
int tmp = memory[i];
memory[i] = -1;
for (; lastWrite < dataSum; lastWrite++) {
if (memory[lastWrite] == -1) {
memory[lastWrite] = tmp;
break;
}
}
}
exitDaLoop:
for (int i = 0; i < dataSum; i++) {
if (memory[i] != -1)
printf("%u", memory[i]);
else
printf(".");
}
// now hash time
unsigned long int hash = 0;
for (int i = 0; i < dataSum; i++) {
if (memory[i] != -1)
hash += memory[i] * i;
}
free(memory);
printf("\n%lu\n", hash);
}

134
9/mainB.c Normal file
View file

@ -0,0 +1,134 @@
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
int put_it_in(unsigned int *memory, char *diskMap, unsigned int dataID, unsigned long int dataSum)
{
int dataLen = diskMap[dataID * 2] - '0';
int availableIndex = 0;
int avail = 0;
// find start of dataID
int start = 0;
for (int i = 0; i < dataSum; i++) {
if (memory[i] == dataID) {
start = i;
break;
}
}
// find free space
for (int i = 0; i <= start; i++) {
if ( i == start)
return 0;
if (memory[i] != -1) {
availableIndex = 0;
avail = 0;
continue;
}
if (memory[i] == -1) {
if (!availableIndex)
availableIndex = i;
avail++;
}
if (avail >= dataLen)
break;
}
// write the file to this space
for (int i = 0; i < dataLen; i++) {
memory[availableIndex + i] = dataID;
}
// remove the original file
for (int i = dataSum - 1; i >= availableIndex + dataLen; i--) {
if (memory[i] == dataID) {
for (int j = 0; j < dataLen; j++) {
memory[i - j] = -1;
}
return 1;
}
}
return 0;
}
int main ()
{
// figure out data dimensions
FILE *input = fopen("./input", "r");
int width = 0;
bool flip = 1;
unsigned long int dataSum = 0;
unsigned int dataID = 0;
int dataSlot = 0;
for (char c = fgetc(input); c != EOF && c != '\n'; c = fgetc(input)) {
dataSum += c - '0';
width++;
}
rewind(input);
printf("Length: %d\ndataSum: %u\n", width, dataSum);
// save diskMap, cuz diskMap[dataID * 2] is the file length
unsigned int *memory = malloc(dataSum * sizeof(int));
char *diskMap = malloc(width * sizeof(char));
//
// -1 represents empty memory
for (int i = 0; i < dataSum; i++) {
memory[i] = -1;
}
// time to fill the array
for (int i = 0; i < width; i++) {
char c = fgetc(input);
diskMap[i] = c;
if (c == EOF) {
printf("breakbreakbreak!\n");
break;
}
// fan ska ju bara vara varannan som är data, resten free space
// flipflopflipflop ^
for (int j = 0; j < (c - '0'); j++) {
if (flip) {
memory[dataSlot] = dataID;
printf("%u", memory[dataSlot]);
} else {
memory[dataSlot] = -1;
printf(".");
}
dataSlot++;
}
if (flip)
dataID++;
flip ^= true;
}
fclose(input);
printf("\n");
// it's compacting time!
// start at end of array, save id, overwrite, put in first free spot
unsigned int lastID = - 1;
for (int i = dataSum - 1; i >= 0; i--) {
if (memory[i] < lastID) {
lastID = memory[i];
put_it_in(memory, diskMap, lastID, dataSum);
}
}
for (int i = 0; i < dataSum; i++) {
if (memory[i] != -1)
printf("%u ", memory[i]);
else
printf(".");
}
// now hash time
unsigned long int hash = 0;
for (int i = 0; i < dataSum; i++) {
if (memory[i] != -1)
hash += memory[i] * i;
}
free(memory);
printf("\n%lu\n", hash);
}

5
9/testout Normal file

File diff suppressed because one or more lines are too long