Skip to content

Instantly share code, notes, and snippets.

@Aadv1k
Created September 7, 2025 18:38
Show Gist options
  • Select an option

  • Save Aadv1k/a36e3cb32b8dd9a0929637bba193e8b1 to your computer and use it in GitHub Desktop.

Select an option

Save Aadv1k/a36e3cb32b8dd9a0929637bba193e8b1 to your computer and use it in GitHub Desktop.
Advent Of Code 2024 in C DAY 01 02 (one star only) 03
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int comparator(const void* a, const void* b) {
return (*(int*)a - *(int*)b);
}
int main(void) {
FILE* f = fopen("input.txt", "r");
if (f == NULL) {
fprintf(stderr, "Could not open file!\n");
return 0;
}
/** We do this hackery because we know AOC inputs are predictable, and have
* the same line no **/
char buf[512];
fgets(buf, sizeof(buf), f);
const int line_len = strlen(buf);
fseek(f, -1, SEEK_END);
const int line_count = (ftell(f) / line_len) + 1;
rewind(f);
/*******/
int x, y;
int* lhs = malloc(sizeof(int)*line_count);
int* rhs = malloc(sizeof(int)*line_count);
int offset = 0;
while (fscanf(f, "%d\t%d", (lhs+offset), (rhs+offset)) != EOF) offset++;
qsort(lhs, line_count, sizeof(int), comparator);
qsort(rhs, line_count, sizeof(int), comparator);
int p1_result = 0;
for (int i = 0; i < line_count; ++i) {
p1_result += abs(lhs[i] - rhs[i]);
}
printf("Part 1 result: %d\n", p1_result);
/* part 2 */
int p2_result = 0;
int mult;
for (int i = 0; i < line_count; ++i) {
mult = 0;
for (int j = 0; j < line_count; ++j ) {
if (lhs[i] == rhs[j]) {
mult+=1;
}
}
p2_result += (lhs[i] * mult);
}
printf("Part 2 result: %d\n", p2_result);
free(lhs);
free(rhs);
fclose(f);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
int check_is_safe(char* buf) {
char const* sep = " ";
const size_t len = strlen(buf);
char* tok = strtok(buf, sep);
int last_diff = 0, last;
bool increasing, is_bad = false, first_iter = true;
int i = 0;
while (tok != NULL) {
int cur;
if (first_iter) {
last = atoi(tok);
first_iter = false;
tok = strtok(NULL, sep);
continue;
}
cur = atoi(tok);
int const r_diff = last - cur;
int const diff = abs(r_diff);
if ((diff < 1 || diff > 3) ||
(last_diff > 0 && r_diff < 0) ||
(last_diff < 0 && r_diff > 0))
{
return i;
}
last = cur;
last_diff = r_diff;
tok = strtok(NULL, sep);
i++;
}
return -1;
}
void splice(char* new, const char* buf, int offset) {
char buf_cpy[1024];
strcpy(buf_cpy, buf);
char pref[1024];
strncpy(pref, buf_cpy, sizeof(char)*offset);
pref[offset] = '\0';
int offset_end = 0;
for (int i = offset; i < strlen(buf_cpy); ++i) {
if (isspace(buf_cpy[i])) {
offset_end = i;
break;
}
}
char* b_ptr = &buf_cpy[offset_end];
sprintf(new, "%s%s", pref, b_ptr);
}
int main(void) {
FILE* f = fopen("./input_d2.txt", "r");
if (f == NULL) {
fprintf(stderr, "Could not open file!\n");
return 0;
}
char buf[1024];
int safe_r = 0, safe_r_with_dampening = 0;
while (fgets(buf, sizeof(buf), f) != NULL) {
char buf_cpy[1024];
strcpy(buf_cpy, buf);
int point_of_contention = check_is_safe(buf);
int offset = point_of_contention+1;
if (point_of_contention == -1) {
safe_r++;
continue;
}
char new_cpy[1024];
splice(new_cpy, buf_cpy, offset);
int new_offset = check_is_safe(new_cpy)+1;
if (new_offset == 0) {
safe_r_with_dampening++;
continue;
}
char new_new_cpy[1024];
splice(new_new_cpy, new_cpy, new_offset);
if (check_is_safe(new_new_cpy) == -1) {
safe_r_with_dampening++;
continue;
}
}
printf("Safe reports: %d\n", safe_r);
printf("Safe reports with dapening: %d\n", safe_r_with_dampening);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
#include <ctype.h>
#include <stdbool.h>
int get_digit_offset(char* buf, int start, int len) {
for (int i = start; i < len; ++i) {
if (!isdigit((unsigned char)buf[i])) return i;
}
return -1;
}
int main(void) {
FILE* f = fopen("input_d3.txt", "r");
if (f == NULL) {
fprintf(stderr, "Could not open file!\n");
return 0;
}
char buf[4096];
int sum_p1 = 0;
int sum_p2 = 0;
bool enabled = true;
while (fgets(buf, sizeof(buf), f) != NULL) {
for (size_t i = 0; i < strlen(buf); ++i) {
// d o n ' t
if (i+4 < strlen(buf) && buf[i] == 'd' && buf[i+1] == 'o') {
if (buf[i+2] == 'n' &&
buf[i+3] == '\'' &&
buf[i+4] == 't' &&
buf[i+5] == '(' &&
buf[i+6] == ')'
) {
enabled = false;
}
if (buf[i+2] == '(' && buf[i+3] == ')') {
enabled = true;
}
}
if (i+3 < strlen(buf) &&
buf[i] == 'm' &&
buf[i+1] == 'u' &&
buf[i+2] == 'l' &&
buf[i+3] == '(') {
int x1 = get_digit_offset(buf, i+4, strlen(buf));
// basically the digit next the ( wasn't a digit
if (x1 == -1) continue;
if (buf[x1] != ',') continue;
// the offset will put us right at the comma
int x2 = get_digit_offset(buf, x1+1, strlen(buf));
if (x2 == -1) continue;
if (buf[x2] != ')') continue;
char buf_inner[2048*2];
strncpy(buf_inner, (&buf[i+4]), x2);
buf_inner[x2-(i+4)] = '\0';
int x, y;
sscanf(buf_inner, "%d,%d", &x, &y);
sum_p1 += x*y;
if (enabled) sum_p2 += x*y;
i = x2;
}
}
}
printf("Part 1 Answer: %d\n", sum_p1);
printf("Part 2 Answer: %d\n", sum_p2);
fclose(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment