1
0

solve day 6

This commit is contained in:
Rokas Puzonas 2022-12-06 22:58:18 +02:00
parent 78dc6e8d2c
commit bd9cf44c2d
2 changed files with 57 additions and 0 deletions

56
day6.c Normal file
View File

@ -0,0 +1,56 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "aoc.h"
#define MESSAGE_LENGTH 14
static void *day6_parse(char **lines, int line_count)
{
return lines[0];
}
static void day6_part1(void *p)
{
char *msg = p;
int n = strlen(msg);
for (int i = 3; i < n; i++) {
char c1 = msg[i-3];
char c2 = msg[i-2];
char c3 = msg[i-1];
char c4 = msg[i-0];
if (c1 != c2 && c1 != c3 && c1 != c4 &&
c2 != c3 && c2 != c4 &&
c3 != c4) {
printf("%d\n", i+1);
break;
}
}
}
static bool is_start_of_message(char *str)
{
for (int i=0; i < MESSAGE_LENGTH - 1; i++) {
for (int j=i+1; j < MESSAGE_LENGTH; j++) {
if (str[i] == str[j]) {
return false;
}
}
}
return true;
}
static void day6_part2(void *p)
{
char *msg = p;
int n = strlen(msg);
for (int i = 0; i < n-13; i++) {
if (is_start_of_message(msg+i)) {
printf("%d\n", i+14);
break;
}
}
}
ADD_SOLUTION(6, day6_parse, day6_part1, day6_part2);

1
main.c
View File

@ -13,6 +13,7 @@
#include "day3.c"
#include "day4.c"
#include "day5.c"
#include "day6.c"
Solution *find_solution(int day)
{