QUOTE: Never too old to chase dreams.

aoc

Advent of Code challenges

aoc_2024_8_1.c (2468B)


      1#include <stdlib.h>
      2#include <stdbool.h>
      3#include <string.h>
      4#include <assert.h>
      5
      6#define ANTENNA_SIZE 500
      7#define ANTINODE_SIZE 500
      8
      9typedef struct {
     10  int y;
     11  int x;
     12} pos_t;
     13
     14typedef struct {
     15  char id;
     16  pos_t pos;
     17} antenna_t;
     18
     19long perform(char *data) {
     20  size_t len = strlen(data);
     21  size_t rows = 0;
     22  size_t cols = 0;
     23  size_t antennas_len = 0;
     24  antenna_t antennas[ANTENNA_SIZE];
     25  for (size_t i = 0; i < len; i++) {
     26    if (data[i] == '\n') {
     27      if (rows == 0) {
     28        cols = i;
     29      }
     30      rows++;
     31    }
     32  }
     33  for (size_t i = 0; i < len; i++) {
     34    char c = data[i];
     35    if (c != '\n' && c != '.') {
     36      int y = i / (cols + 1);
     37      int x = i % (cols + 1);
     38      assert(antennas_len < ANTENNA_SIZE);
     39      antennas[antennas_len] = (antenna_t) {
     40        .id = c,
     41        .pos = { y, x },
     42      };
     43      antennas_len++;
     44    }
     45  }
     46  size_t antinodes_len = 0;
     47  pos_t antinodes[ANTINODE_SIZE];
     48  for (size_t i = 0; i < antennas_len; i++) {
     49    for (size_t j = i + 1; j < antennas_len; j++) {
     50      antenna_t a = antennas[i];
     51      antenna_t b = antennas[j];
     52      if (a.id == b.id) {
     53        pos_t delta = {
     54          abs(a.pos.y - b.pos.y),
     55          abs(a.pos.x - b.pos.x),
     56        };
     57        pos_t node_a;
     58        pos_t node_b;
     59        if (a.pos.y > b.pos.y) {
     60          node_a.y = a.pos.y + delta.y;
     61          node_b.y = b.pos.y - delta.y;
     62        }
     63        else {
     64          node_a.y = a.pos.y - delta.y;
     65          node_b.y = b.pos.y + delta.y;
     66        }
     67        if (a.pos.x > b.pos.x) {
     68          node_a.x = a.pos.x + delta.x;
     69          node_b.x = b.pos.x - delta.x;
     70        }
     71        else {
     72          node_a.x = a.pos.x - delta.x;
     73          node_b.x = b.pos.x + delta.x;
     74        }
     75        assert(antinodes_len < ANTINODE_SIZE);
     76        antinodes[antinodes_len] = node_a;
     77        antinodes_len++;
     78        assert(antinodes_len < ANTINODE_SIZE);
     79        antinodes[antinodes_len] = node_b;
     80        antinodes_len++;
     81      }
     82    }
     83  }
     84  long count = 0;
     85  for (size_t i = 0; i < antinodes_len; i++) {
     86    pos_t antinode = antinodes[i];
     87    if (
     88      antinode.y < 0 || antinode.x < 0 ||
     89      antinode.y >= (int) rows || antinode.x >= (int) cols
     90    ) {
     91      continue;
     92    }
     93    bool is_unique = true;
     94    for (size_t j = i + 1; j < antinodes_len; j++) {
     95      if (antinode.y == antinodes[j].y && antinode.x == antinodes[j].x) {
     96        is_unique = false;
     97        break;
     98      }
     99    }
    100    if (is_unique) {
    101      count++;
    102    }
    103  }
    104  return count;
    105}