Day8
This commit is contained in:
BIN
day8/c/day8
Executable file
BIN
day8/c/day8
Executable file
Binary file not shown.
133
day8/c/day8.c
Normal file
133
day8/c/day8.c
Normal file
@@ -0,0 +1,133 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAX_DIMENSION 1024
|
||||
#define MAX_ANTENNA_KINDS 128
|
||||
#define MAX_ANTENNA_PER_KIND 128
|
||||
|
||||
typedef struct antennas {
|
||||
char antenna_kind[MAX_ANTENNA_KINDS];
|
||||
int antennas[MAX_ANTENNA_KINDS][MAX_ANTENNA_PER_KIND][2];
|
||||
int antenna_count[MAX_ANTENNA_KINDS];
|
||||
int count;
|
||||
} antennas_t;
|
||||
|
||||
int main() {
|
||||
char c;
|
||||
|
||||
char **map = calloc(MAX_DIMENSION, sizeof(map[0]));
|
||||
int rows = 0, columns, i = 0;
|
||||
|
||||
map[0] = calloc(MAX_DIMENSION, sizeof(map[0][0]));
|
||||
|
||||
while ((c = getchar()) != EOF) {
|
||||
map[rows][i] = c;
|
||||
i++;
|
||||
if (c != '\n') {
|
||||
continue;
|
||||
}
|
||||
columns = i - 1;
|
||||
rows++;
|
||||
|
||||
map[rows] = calloc(MAX_DIMENSION, sizeof(map[0][0]));
|
||||
i = 0;
|
||||
}
|
||||
|
||||
|
||||
antennas_t antennas = { 0 };
|
||||
|
||||
for (i = 0; i < rows; i++) {
|
||||
for (int j = 0; j < columns; j++) {
|
||||
switch (map[i][j]) {
|
||||
case '.':
|
||||
break;
|
||||
default:
|
||||
{
|
||||
int idx;
|
||||
for (idx = 0; idx < antennas.count; idx++) {
|
||||
if (antennas.antenna_kind[idx] == map[i][j]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (antennas.count == idx) {
|
||||
antennas.count++;
|
||||
}
|
||||
antennas.antenna_kind[idx] = map[i][j];
|
||||
antennas.antennas[idx][antennas.antenna_count[idx]][0] = i;
|
||||
antennas.antennas[idx][antennas.antenna_count[idx]][1] = j;
|
||||
antennas.antenna_count[idx]++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int **antinodes = calloc(rows, sizeof(antinodes[0]));
|
||||
for (i = 0; i < rows; i++) {
|
||||
antinodes[i] = calloc(columns, sizeof(antinodes[0][0]));
|
||||
}
|
||||
int **antinodes2 = calloc(rows, sizeof(antinodes2[0]));
|
||||
for (i = 0; i < rows; i++) {
|
||||
antinodes2[i] = calloc(columns, sizeof(antinodes2[0][0]));
|
||||
}
|
||||
|
||||
for (i = 0; i < antennas.count; i++) {
|
||||
for (int j = 0; j < antennas.antenna_count[i]; j++) {
|
||||
for (int k = j + 1; k < antennas.antenna_count[i]; k++) {
|
||||
int x1 = antennas.antennas[i][j][0];
|
||||
int y1 = antennas.antennas[i][j][1];
|
||||
int x2 = antennas.antennas[i][k][0];
|
||||
int y2 = antennas.antennas[i][k][1];
|
||||
int diffx = (x2 - x1);
|
||||
int diffy = (y2 - y1);
|
||||
for (int l = 0; 1; l++) {
|
||||
int ax1 = x2 + l * diffx;
|
||||
int ay1 = y2 + l * diffy;
|
||||
if (!(ax1 >= 0 && ax1 < rows && ay1 >= 0 && ay1 < columns)) {
|
||||
break;
|
||||
}
|
||||
if (l == 1) {
|
||||
antinodes[ax1][ay1] = 1;
|
||||
}
|
||||
antinodes2[ax1][ay1] = 1;
|
||||
}
|
||||
for (int l = 0; 1; l++) {
|
||||
int ax2 = x1 - l * diffx;
|
||||
int ay2 = y1 - l * diffy;
|
||||
if (!(ax2 >= 0 && ax2 < rows && ay2 >= 0 && ay2 < columns)) {
|
||||
break;
|
||||
}
|
||||
if (l == 1) {
|
||||
antinodes[ax2][ay2] = 1;
|
||||
}
|
||||
antinodes2[ax2][ay2] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
int count2 = 0;
|
||||
for (i = 0; i < rows; i++) {
|
||||
for (int j = 0; j < columns; j++) {
|
||||
count += antinodes[i][j];
|
||||
count2 += antinodes2[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < rows + 1; i++) {
|
||||
free(antinodes[i]);
|
||||
}
|
||||
free(antinodes);
|
||||
for (i = 0; i < rows + 1; i++) {
|
||||
free(antinodes2[i]);
|
||||
}
|
||||
free(antinodes2);
|
||||
for (i = 0; i < rows + 1; i++) {
|
||||
free(map[i]);
|
||||
}
|
||||
free(map);
|
||||
|
||||
printf("%i\n", count);
|
||||
printf("%i\n", count2);
|
||||
}
|
||||
50
day8/input.txt
Normal file
50
day8/input.txt
Normal file
@@ -0,0 +1,50 @@
|
||||
.........................p........................
|
||||
......................h....C............M.........
|
||||
..............................p....U..............
|
||||
..5..................p............................
|
||||
..6z...........................................C..
|
||||
...............c...........zV.....................
|
||||
...5.....c........................................
|
||||
.Z.............h........S...z....9................
|
||||
.O............................9...z........M..C...
|
||||
..O....5..............................F..M..C.....
|
||||
..Z.........5.c...............M....V..............
|
||||
........ZO................q.......................
|
||||
s...O................h..Uq.....7V...........4.....
|
||||
.q.g..............c.............p.......4.........
|
||||
............hZ.............................4G.....
|
||||
6s...........................U.Q.....3............
|
||||
.......6.................9.......Q.............3..
|
||||
....s..D.........................6................
|
||||
.............................................FL...
|
||||
..................................................
|
||||
..g...D.........q.....f.......Q...F....L......7...
|
||||
...............2.........f.............V.L...4....
|
||||
...................2.s...................f3......G
|
||||
....g...........................v......7P.........
|
||||
..2..g.............d.....v...........P.......1....
|
||||
..............u.........f.............L........G..
|
||||
.........l.D....u...............d........o..P.....
|
||||
..................8...............9..1......o...7.
|
||||
............l.....................................
|
||||
...................l...S...........F.......o..U...
|
||||
.......................u...S......................
|
||||
..........l....u...............m...........P....G.
|
||||
......................................1.8.......o.
|
||||
..................................................
|
||||
..................v.......S................0......
|
||||
.............v........d.....1.....................
|
||||
..................................................
|
||||
..........D....................................0..
|
||||
...................m.............H..........0.....
|
||||
...................................d......0.......
|
||||
..................................................
|
||||
....Q.............................................
|
||||
................................H.................
|
||||
........................H....................8....
|
||||
..................................................
|
||||
..................................................
|
||||
.........................................8........
|
||||
.......................H3.........................
|
||||
............................m.....................
|
||||
................................m.................
|
||||
12
day8/sample.txt
Normal file
12
day8/sample.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
............
|
||||
........0...
|
||||
.....0......
|
||||
.......0....
|
||||
....0.......
|
||||
......A.....
|
||||
............
|
||||
............
|
||||
........A...
|
||||
.........A..
|
||||
............
|
||||
............
|
||||
Reference in New Issue
Block a user