73 lines
1.7 KiB
C
73 lines
1.7 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "../../lib/aoc.h"
|
|
|
|
int compare(const void *a, const void *b) {
|
|
return *(int*)a - *(int*)b;
|
|
}
|
|
|
|
int visited_houses(char *input, int num_santas) {
|
|
int moves_count = strlen(input);
|
|
int *positions = calloc(moves_count + 1, sizeof(int)); // +1 for the starting position
|
|
int **santa_positions = calloc(num_santas, sizeof(int*));
|
|
for (int i = 0; i < num_santas; i++) {
|
|
santa_positions[i] = calloc(2, sizeof(int));
|
|
santa_positions[i][0] = 0;
|
|
santa_positions[i][1] = 0;
|
|
}
|
|
int santa_index = 0;
|
|
int *santa_position = santa_positions[0];
|
|
|
|
positions[0] = 0;
|
|
int index = 0;
|
|
|
|
while (input[index] > 10) {
|
|
switch(input[index++]) {
|
|
case '^':
|
|
santa_position[1] += 1;
|
|
break;
|
|
case 'v':
|
|
santa_position[1] -= 1;
|
|
break;
|
|
case '>':
|
|
santa_position[0] += 1;
|
|
break;
|
|
case '<':
|
|
santa_position[0] -= 1;
|
|
break;
|
|
}
|
|
|
|
positions[index] = (santa_position[0] * moves_count) + santa_position[1];
|
|
santa_index = (santa_index + 1) % num_santas;
|
|
santa_position = santa_positions[santa_index];
|
|
}
|
|
|
|
qsort(positions, moves_count, sizeof(int), compare);
|
|
int last_position = positions[0];
|
|
int num_visited_houses = 0;
|
|
|
|
for (int i = 0; i < moves_count + 1; i++) {
|
|
if (positions[i] != last_position) {
|
|
num_visited_houses++;
|
|
last_position = positions[i];
|
|
}
|
|
}
|
|
|
|
free(positions);
|
|
for (int i = 0; i < num_santas; i++) {
|
|
free(santa_positions[i]);
|
|
}
|
|
free(santa_positions);
|
|
return num_visited_houses;
|
|
}
|
|
|
|
int main() {
|
|
char *input = aoc_read_input();
|
|
|
|
printf("Part 1: %d\n", visited_houses(input, 1));
|
|
printf("Part 2: %d\n", visited_houses(input, 2));
|
|
|
|
free(input);
|
|
}
|