2024 day18
This commit is contained in:
parent
307e990924
commit
d8f796c8cf
@ -64,12 +64,14 @@ int64_t _2024_day16_part1(const char * input, int length)
|
|||||||
struct dijkstra_xy16 prev[width * height];
|
struct dijkstra_xy16 prev[width * height];
|
||||||
int dist[width * height];
|
int dist[width * height];
|
||||||
|
|
||||||
|
int turn_cost = 1000;
|
||||||
dijkstra_cartesian(input,
|
dijkstra_cartesian(input,
|
||||||
stride,
|
stride,
|
||||||
width, height,
|
width, height,
|
||||||
start_x, start_y,
|
start_x, start_y,
|
||||||
end_x, end_y,
|
end_x, end_y,
|
||||||
'#',
|
'#',
|
||||||
|
turn_cost,
|
||||||
prev,
|
prev,
|
||||||
dist);
|
dist);
|
||||||
|
|
||||||
|
3450
2024/day18/input.txt
Normal file
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
15
2024/day18/input.txt.h
Normal 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
25
2024/day18/sample1.txt
Normal 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
15
2024/day18/sample1.txt.h
Normal 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
141
2024/day18/solution.c
Normal 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;
|
||||||
|
}
|
10
dijkstra.c
10
dijkstra.c
@ -13,6 +13,7 @@ void dijkstra_cartesian(const char * graph,
|
|||||||
int start_x, int start_y,
|
int start_x, int start_y,
|
||||||
int end_x, int end_y,
|
int end_x, int end_y,
|
||||||
char wall,
|
char wall,
|
||||||
|
int turn_cost,
|
||||||
struct dijkstra_xy16 * prev,
|
struct dijkstra_xy16 * prev,
|
||||||
int * dist)
|
int * dist)
|
||||||
{
|
{
|
||||||
@ -49,15 +50,8 @@ void dijkstra_cartesian(const char * graph,
|
|||||||
struct dijkstra_xy16 * xy = &prev[min.y * width + min.x];
|
struct dijkstra_xy16 * xy = &prev[min.y * width + min.x];
|
||||||
int dx = min.x - xy->x;
|
int dx = min.x - xy->x;
|
||||||
int dy = min.y - xy->y;
|
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)
|
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;
|
int alt = dist[min.y * width + min.x] + 1 + turn;
|
||||||
if (alt < dist[ny * width + nx]) {
|
if (alt < dist[ny * width + nx]) {
|
||||||
|
@ -17,6 +17,7 @@ void dijkstra_cartesian(const char * graph,
|
|||||||
int start_x, int start_y,
|
int start_x, int start_y,
|
||||||
int end_x, int end_y,
|
int end_x, int end_y,
|
||||||
char wall,
|
char wall,
|
||||||
|
int turn_cost,
|
||||||
struct dijkstra_xy16 * prev,
|
struct dijkstra_xy16 * prev,
|
||||||
int * dist);
|
int * dist);
|
||||||
|
|
||||||
|
@ -46,6 +46,8 @@
|
|||||||
#include "2024/day17/sample1.txt.h"
|
#include "2024/day17/sample1.txt.h"
|
||||||
#include "2024/day17/sample2.txt.h"
|
#include "2024/day17/sample2.txt.h"
|
||||||
#include "2024/day17/input.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] = {
|
static struct start_size sample[][2] = {
|
||||||
{
|
{
|
||||||
@ -180,6 +182,12 @@ static struct start_size sample[][2] = {
|
|||||||
{ ( char *)&_binary_2024_day17_sample2_txt_start,
|
{ ( char *)&_binary_2024_day17_sample2_txt_start,
|
||||||
(uint32_t)&_binary_2024_day17_sample2_txt_size },
|
(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[] = {
|
static struct start_size input[] = {
|
||||||
@ -227,4 +235,6 @@ static struct start_size input[] = {
|
|||||||
(uint32_t)&_binary_2024_day16_input_txt_size },
|
(uint32_t)&_binary_2024_day16_input_txt_size },
|
||||||
{ ( char *)&_binary_2024_day17_input_txt_start,
|
{ ( char *)&_binary_2024_day17_input_txt_start,
|
||||||
(uint32_t)&_binary_2024_day17_input_txt_size },
|
(uint32_t)&_binary_2024_day17_input_txt_size },
|
||||||
|
{ ( char *)&_binary_2024_day18_input_txt_start,
|
||||||
|
(uint32_t)&_binary_2024_day18_input_txt_size },
|
||||||
};
|
};
|
||||||
|
2
runner.c
2
runner.c
@ -38,7 +38,7 @@ bool runner_tick(struct runner_state * runner_state)
|
|||||||
int year = solution[ix].year;
|
int year = solution[ix].year;
|
||||||
int day = solution[ix].day;
|
int day = solution[ix].day;
|
||||||
|
|
||||||
if (year != 2024 || day != 17) {
|
if (year != 2024 || day != 18) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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_day16_part2(const char * input, int length);
|
||||||
int64_t _2024_day17_part1(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_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[] = {
|
struct day_funcs solution[] = {
|
||||||
{
|
{
|
||||||
@ -160,4 +162,9 @@ struct day_funcs solution[] = {
|
|||||||
{_2024_day17_part1, _2024_day17_part2},
|
{_2024_day17_part1, _2024_day17_part2},
|
||||||
NULL,
|
NULL,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
2024, 18,
|
||||||
|
{_2024_day18_part1, _2024_day18_part2},
|
||||||
|
NULL,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
@ -70,4 +70,7 @@ DAY_OBJ = \
|
|||||||
2024/day17/sample1.txt.o \
|
2024/day17/sample1.txt.o \
|
||||||
2024/day17/sample2.txt.o \
|
2024/day17/sample2.txt.o \
|
||||||
2024/day17/input.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
|
||||||
|
@ -39,12 +39,14 @@ static bool dijkstra_test_0(const char ** scenario)
|
|||||||
struct dijkstra_xy16 prev[width * height];
|
struct dijkstra_xy16 prev[width * height];
|
||||||
int dist[width * height];
|
int dist[width * height];
|
||||||
|
|
||||||
|
int turn_cost = 100;
|
||||||
dijkstra_cartesian(graph,
|
dijkstra_cartesian(graph,
|
||||||
stride,
|
stride,
|
||||||
width, height,
|
width, height,
|
||||||
end_x, end_y,
|
end_x, end_y,
|
||||||
start_x, start_y,
|
start_x, start_y,
|
||||||
'#',
|
'#',
|
||||||
|
turn_cost,
|
||||||
prev,
|
prev,
|
||||||
dist);
|
dist);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user