2024 day18

This commit is contained in:
Zack Buhman 2024-12-20 00:40:34 -06:00
parent 307e990924
commit d8f796c8cf
13 changed files with 3675 additions and 10 deletions

View File

@ -64,12 +64,14 @@ int64_t _2024_day16_part1(const char * input, int length)
struct dijkstra_xy16 prev[width * height];
int dist[width * height];
int turn_cost = 1000;
dijkstra_cartesian(input,
stride,
width, height,
start_x, start_y,
end_x, end_y,
'#',
turn_cost,
prev,
dist);

3450
2024/day18/input.txt Normal file

File diff suppressed because it is too large Load Diff

15
2024/day18/input.txt.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_2024_day18_input_txt_start __asm("_binary_2024_day18_input_txt_start");
extern uint32_t _binary_2024_day18_input_txt_end __asm("_binary_2024_day18_input_txt_end");
extern uint32_t _binary_2024_day18_input_txt_size __asm("_binary_2024_day18_input_txt_size");
#ifdef __cplusplus
}
#endif

25
2024/day18/sample1.txt Normal file
View File

@ -0,0 +1,25 @@
5,4
4,2
4,5
3,0
2,1
6,3
2,4
1,5
0,6
3,3
2,6
5,1
1,2
5,5
2,5
6,5
1,4
0,4
6,4
1,1
6,1
1,0
0,5
1,6
2,0

15
2024/day18/sample1.txt.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_2024_day18_sample1_txt_start __asm("_binary_2024_day18_sample1_txt_start");
extern uint32_t _binary_2024_day18_sample1_txt_end __asm("_binary_2024_day18_sample1_txt_end");
extern uint32_t _binary_2024_day18_sample1_txt_size __asm("_binary_2024_day18_sample1_txt_size");
#ifdef __cplusplus
}
#endif

141
2024/day18/solution.c Normal file
View File

@ -0,0 +1,141 @@
#include <stdint.h>
#include "parse.h"
#include "dijkstra.h"
#include "memory.h"
#include "printf.h"
struct coordinate {
int x;
int y;
};
static const char * parse_coordinate(const char * input, struct coordinate * c)
{
input = parse_base10(input, &c->x);
input = parse_skip(input, ',');
input = parse_base10(input, &c->y);
input = parse_skip(input, '\n');
return input;
}
static char map_char(const char c)
{
if (c == 0)
return '.';
else if (c == 1)
return '#';
else
return 'O';
}
static void print_map(const char * map, int stride)
{
for (int y = 0; y < stride; y++) {
for (int x = 0; x < stride; x++) {
print_char(map_char(map[y * stride + x]));
}
print_char('\n');
}
}
int64_t _2024_day18_part1(const char * input, int length)
{
const char * end = input + length;
struct coordinate start_c = {0, 0};
//struct coordinate end_c = {6, 6};
struct coordinate end_c = {70, 70};
//int stride = 7;
int stride = 71;
char map[stride * stride];
memory_set_char(map, 0, stride * stride);
int i = 0;
while (input < end) {
if (i >= 1024)
break;
struct coordinate c;
input = parse_coordinate(input, &c);
map[c.y * stride + c.x] = 1;
i++;
}
struct dijkstra_xy16 prev[stride * stride];
int dist[stride * stride];
int wall = 1;
int turn_cost = 0;
dijkstra_cartesian(map,
stride,
stride, stride,
start_c.x, start_c.y,
end_c.x, end_c.y,
wall,
turn_cost,
prev,
dist);
/*
int x = end_c.x;
int y = end_c.y;
while (!(x == start_c.x && y == start_c.y)) {
struct dijkstra_xy16 * xy = &prev[y * stride + x];
map[xy->y * stride + xy->x] = 2;
x = xy->x;
y = xy->y;
}
print_map(map, stride);
*/
return dist[end_c.y * stride + end_c.x];
}
int64_t _2024_day18_part2(const char * input, int length)
{
const char * end = input + length;
struct coordinate start_c = {0, 0};
//struct coordinate end_c = {6, 6};
struct coordinate end_c = {70, 70};
//int stride = 7;
int stride = 71;
char map[stride * stride];
memory_set_char(map, 0, stride * stride);
int i = 0;
struct coordinate c;
while (input < end) {
input = parse_coordinate(input, &c);
map[c.y * stride + c.x] = 1;
i++;
if (i > 2900) {
printf("%d ", i);
struct dijkstra_xy16 prev[stride * stride];
int dist[stride * stride];
int wall = 1;
int turn_cost = 0;
dijkstra_cartesian(map,
stride,
stride, stride,
start_c.x, start_c.y,
end_c.x, end_c.y,
wall,
turn_cost,
prev,
dist);
if (prev[end_c.y * stride + end_c.x].x == -1)
break;
}
}
printf("\n%d,%d\n", c.x, c.y);
return -1;
}

View File

@ -13,6 +13,7 @@ void dijkstra_cartesian(const char * graph,
int start_x, int start_y,
int end_x, int end_y,
char wall,
int turn_cost,
struct dijkstra_xy16 * prev,
int * dist)
{
@ -49,15 +50,8 @@ void dijkstra_cartesian(const char * graph,
struct dijkstra_xy16 * xy = &prev[min.y * width + min.x];
int dx = min.x - xy->x;
int dy = min.y - xy->y;
if (min.y == 137) {
printf("xy % 2d % 2d pxy % 2d % 2d nxy % 2d % 2d dist %d\n",
min.x, min.y,
xy->x, xy->y,
nx, ny,
dist[min.y * width + min.x]);
}
if (cartesian_neighbor[i].x != dx || cartesian_neighbor[i].y != dy)
turn = 1000;
turn = turn_cost;
int alt = dist[min.y * width + min.x] + 1 + turn;
if (alt < dist[ny * width + nx]) {

View File

@ -17,6 +17,7 @@ void dijkstra_cartesian(const char * graph,
int start_x, int start_y,
int end_x, int end_y,
char wall,
int turn_cost,
struct dijkstra_xy16 * prev,
int * dist);

View File

@ -46,6 +46,8 @@
#include "2024/day17/sample1.txt.h"
#include "2024/day17/sample2.txt.h"
#include "2024/day17/input.txt.h"
#include "2024/day18/sample1.txt.h"
#include "2024/day18/input.txt.h"
static struct start_size sample[][2] = {
{
@ -180,6 +182,12 @@ static struct start_size sample[][2] = {
{ ( char *)&_binary_2024_day17_sample2_txt_start,
(uint32_t)&_binary_2024_day17_sample2_txt_size },
},
{
{ ( char *)&_binary_2024_day18_sample1_txt_start,
(uint32_t)&_binary_2024_day18_sample1_txt_size },
{ ( char *)&_binary_2024_day18_sample1_txt_start,
(uint32_t)&_binary_2024_day18_sample1_txt_size },
},
};
static struct start_size input[] = {
@ -227,4 +235,6 @@ static struct start_size input[] = {
(uint32_t)&_binary_2024_day16_input_txt_size },
{ ( char *)&_binary_2024_day17_input_txt_start,
(uint32_t)&_binary_2024_day17_input_txt_size },
{ ( char *)&_binary_2024_day18_input_txt_start,
(uint32_t)&_binary_2024_day18_input_txt_size },
};

View File

@ -38,7 +38,7 @@ bool runner_tick(struct runner_state * runner_state)
int year = solution[ix].year;
int day = solution[ix].day;
if (year != 2024 || day != 17) {
if (year != 2024 || day != 18) {
return false;
}

View File

@ -48,6 +48,8 @@ int64_t _2024_day16_part1(const char * input, int length);
int64_t _2024_day16_part2(const char * input, int length);
int64_t _2024_day17_part1(const char * input, int length);
int64_t _2024_day17_part2(const char * input, int length);
int64_t _2024_day18_part1(const char * input, int length);
int64_t _2024_day18_part2(const char * input, int length);
struct day_funcs solution[] = {
{
@ -160,4 +162,9 @@ struct day_funcs solution[] = {
{_2024_day17_part1, _2024_day17_part2},
NULL,
},
{
2024, 18,
{_2024_day18_part1, _2024_day18_part2},
NULL,
},
};

View File

@ -70,4 +70,7 @@ DAY_OBJ = \
2024/day17/sample1.txt.o \
2024/day17/sample2.txt.o \
2024/day17/input.txt.o \
2024/day17/solution.o
2024/day17/solution.o \
2024/day18/sample1.txt.o \
2024/day18/input.txt.o \
2024/day18/solution.o

View File

@ -39,12 +39,14 @@ static bool dijkstra_test_0(const char ** scenario)
struct dijkstra_xy16 prev[width * height];
int dist[width * height];
int turn_cost = 100;
dijkstra_cartesian(graph,
stride,
width, height,
end_x, end_y,
start_x, start_y,
'#',
turn_cost,
prev,
dist);