2024-06,07

This commit is contained in:
Evan Burkey 2025-01-02 12:12:42 -08:00
parent 2aeb547987
commit b6a0735aba
2 changed files with 110 additions and 7 deletions

View File

@ -1,10 +1,110 @@
#include <advent_utility.h>
#include <lfutility.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lfinput.h" #include "lfinput.h"
void advent2024day06(void) { #define TRAVELED 'X'
char *input = get_input("input/2024/06"); #define BLOCK '#'
printf("Solution for Day 06 of 2024 is not completed yet\n");
free(input); static int sim(Point guard, char **map, const size_t width, const size_t height) {
enum Direction dir = DIR_NORTH;
while (guard.x > 0 && guard.y > 0 && guard.x < width && guard.y < height) {
map[guard.y][guard.x] = TRAVELED;
switch (dir) {
case DIR_NORTH:
if (guard.y == 0) {
break;
}
if (map[guard.y - 1][guard.x] == BLOCK) {
turn_right(&dir);
}
break;
case DIR_EAST:
if (guard.x + 1 == width) {
break;
}
if (map[guard.y][guard.x + 1] == BLOCK) {
turn_right(&dir);
}
break;
case DIR_SOUTH:
if (guard.y + 1 == height) {
break;
}
if (map[guard.y + 1][guard.x] == BLOCK) {
turn_right(&dir);
}
break;
case DIR_WEST:
if (guard.x == 0) {
break;
}
if (map[guard.y][guard.x - 1] == BLOCK) {
turn_right(&dir);
}
break;
}
switch (dir) {
case DIR_NORTH: guard.y--;
break;
case DIR_EAST: guard.x++;
break;
case DIR_SOUTH: guard.y++;
break;
case DIR_WEST: guard.x--;
break;
}
/*
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
printf("%c", map[y][x]);
}
printf("\n");
}
printf("\n");
*/
}
int v = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
//printf("%c", map[y][x]);
if (map[y][x] == TRAVELED) {
++v;
}
}
//printf("\n");
}
return v;
}
void advent2024day06(void) {
size_t height = 0;
char **map = get_lines("input/2024/06", &height);
const size_t width = strlen(map[0]);
Point guard = {.x = 0, .y = 0};
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (map[y][x] == '^') {
guard.x = x;
guard.y = y;
goto GUARD_SEARCH_COMPLETE;
}
}
}
GUARD_SEARCH_COMPLETE:
printf("%d\n", sim(guard, map, width, height));
del_lines(map);
} }

View File

@ -4,7 +4,10 @@
#include "lfinput.h" #include "lfinput.h"
void advent2024day07(void) { void advent2024day07(void) {
char *input = get_input("input/2024/07"); size_t sz = 0;
printf("Solution for Day 07 of 2024 is not completed yet\n"); char **input = get_lines("input/2024/07", &sz);
free(input);
del_lines(input);
} }