solve day 1, part 1
This commit is contained in:
parent
dd24f051aa
commit
3ce05f3b1a
3
Makefile
3
Makefile
@ -16,7 +16,7 @@ CFLAGS += -Wno-error=unused-const-variable
|
|||||||
CFLAGS += -Wno-error=unused-but-set-variable
|
CFLAGS += -Wno-error=unused-but-set-variable
|
||||||
CFLAGS += -Wno-error=unused-variable
|
CFLAGS += -Wno-error=unused-variable
|
||||||
CFLAGS += -I$(MAKEFILE_PATH)/include
|
CFLAGS += -I$(MAKEFILE_PATH)/include
|
||||||
CFLAGS += -I$(MAKEFILE_PATH)/puzzle
|
CFLAGS += -I$(MAKEFILE_PATH)
|
||||||
CXXFLAGS += -fno-exceptions
|
CXXFLAGS += -fno-exceptions
|
||||||
#CFLAGS += -DDEBUG_BUTTONS
|
#CFLAGS += -DDEBUG_BUTTONS
|
||||||
#CFLAGS += -DDEBUG_AXES
|
#CFLAGS += -DDEBUG_AXES
|
||||||
@ -132,6 +132,7 @@ MAIN_OBJS = \
|
|||||||
src/glad.o \
|
src/glad.o \
|
||||||
src/opengl.o \
|
src/opengl.o \
|
||||||
$(patsubst %.glsl,%.glsl.o,$(wildcard src/shader/*.glsl)) \
|
$(patsubst %.glsl,%.glsl.o,$(wildcard src/shader/*.glsl)) \
|
||||||
|
$(patsubst %,%.o,$(shell find puzzle/ -type f -name input)) \
|
||||||
$(GLFW)
|
$(GLFW)
|
||||||
|
|
||||||
main: $(MAIN_OBJS)
|
main: $(MAIN_OBJS)
|
||||||
|
|||||||
19
include/shader/day1.fs.glsl.h
Normal file
19
include/shader/day1.fs.glsl.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern uint32_t _binary_src_shader_day1_fs_glsl_start __asm("_binary_src_shader_day1_fs_glsl_start");
|
||||||
|
extern uint32_t _binary_src_shader_day1_fs_glsl_end __asm("_binary_src_shader_day1_fs_glsl_end");
|
||||||
|
extern uint32_t _binary_src_shader_day1_fs_glsl_size __asm("_binary_src_shader_day1_fs_glsl_size");
|
||||||
|
|
||||||
|
#define src_shader_day1_fs_glsl_start ((const char *)&_binary_src_shader_day1_fs_glsl_start)
|
||||||
|
#define src_shader_day1_fs_glsl_end ((const char *)&_binary_src_shader_day1_fs_glsl_end)
|
||||||
|
#define src_shader_day1_fs_glsl_size (src_shader_day1_fs_glsl_end - src_shader_day1_fs_glsl_start)
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
19
include/shader/day1.vs.glsl.h
Normal file
19
include/shader/day1.vs.glsl.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern uint32_t _binary_src_shader_day1_vs_glsl_start __asm("_binary_src_shader_day1_vs_glsl_start");
|
||||||
|
extern uint32_t _binary_src_shader_day1_vs_glsl_end __asm("_binary_src_shader_day1_vs_glsl_end");
|
||||||
|
extern uint32_t _binary_src_shader_day1_vs_glsl_size __asm("_binary_src_shader_day1_vs_glsl_size");
|
||||||
|
|
||||||
|
#define src_shader_day1_vs_glsl_start ((const char *)&_binary_src_shader_day1_vs_glsl_start)
|
||||||
|
#define src_shader_day1_vs_glsl_end ((const char *)&_binary_src_shader_day1_vs_glsl_end)
|
||||||
|
#define src_shader_day1_vs_glsl_size (src_shader_day1_vs_glsl_end - src_shader_day1_vs_glsl_start)
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
81
src/main.c
81
src/main.c
@ -2,6 +2,8 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "glad.h"
|
#include "glad.h"
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
@ -11,6 +13,11 @@
|
|||||||
#include "shader/test.vs.glsl.h"
|
#include "shader/test.vs.glsl.h"
|
||||||
#include "shader/test.fs.glsl.h"
|
#include "shader/test.fs.glsl.h"
|
||||||
|
|
||||||
|
#include "shader/day1.vs.glsl.h"
|
||||||
|
#include "shader/day1.fs.glsl.h"
|
||||||
|
|
||||||
|
#include "puzzle/2025/01/input.h"
|
||||||
|
|
||||||
int vp_width = 800;
|
int vp_width = 800;
|
||||||
int vp_height = 600;
|
int vp_height = 600;
|
||||||
|
|
||||||
@ -27,6 +34,31 @@ const float triangle_array[] = {
|
|||||||
1, 1
|
1, 1
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static inline int next_power_of_two(double n)
|
||||||
|
{
|
||||||
|
return pow(2, ceil(log2(n)));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint rectangularize_input(const char * buf, int size, int * width_out)
|
||||||
|
{
|
||||||
|
int width = next_power_of_two(sqrt(size));
|
||||||
|
|
||||||
|
void * tmp = malloc(width * width);
|
||||||
|
memcpy(tmp, buf, size);
|
||||||
|
|
||||||
|
uint texture = make_texture(buf,
|
||||||
|
GL_R8, // internalformat
|
||||||
|
width,
|
||||||
|
width,
|
||||||
|
GL_RED,
|
||||||
|
GL_UNSIGNED_BYTE);
|
||||||
|
*width_out = width;
|
||||||
|
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
|
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
|
||||||
@ -60,20 +92,12 @@ int main()
|
|||||||
// textures
|
// textures
|
||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
const char * data =
|
int input_width;
|
||||||
"arst"
|
int input_length = puzzle_2025_01_input_size;
|
||||||
"qwfp"
|
uint texture_input = rectangularize_input(puzzle_2025_01_input_start,
|
||||||
"1234"
|
puzzle_2025_01_input_size,
|
||||||
"6789";
|
&input_width);
|
||||||
int input_width = 4;
|
int input_height = input_width;
|
||||||
int input_height = 4;
|
|
||||||
|
|
||||||
uint texture_input = make_texture(data,
|
|
||||||
GL_R8, // internalformat
|
|
||||||
input_width,
|
|
||||||
input_height,
|
|
||||||
GL_RED,
|
|
||||||
GL_UNSIGNED_BYTE);
|
|
||||||
|
|
||||||
uint texture_framebuffer = make_texture(NULL,
|
uint texture_framebuffer = make_texture(NULL,
|
||||||
GL_RGBA32F,
|
GL_RGBA32F,
|
||||||
@ -95,6 +119,14 @@ int main()
|
|||||||
uint program_test__tex_sampler = glGetUniformLocation(program_test, "tex_sampler");
|
uint program_test__tex_sampler = glGetUniformLocation(program_test, "tex_sampler");
|
||||||
uint program_test__tex_size = glGetUniformLocation(program_test, "tex_size");
|
uint program_test__tex_size = glGetUniformLocation(program_test, "tex_size");
|
||||||
|
|
||||||
|
uint program_day1 = compile_shader(src_shader_day1_vs_glsl_start,
|
||||||
|
src_shader_day1_vs_glsl_size,
|
||||||
|
src_shader_day1_fs_glsl_start,
|
||||||
|
src_shader_day1_fs_glsl_size);
|
||||||
|
uint program_day1__tex_sampler = glGetUniformLocation(program_day1, "tex_sampler");
|
||||||
|
uint program_day1__tex_size = glGetUniformLocation(program_day1, "tex_size");
|
||||||
|
uint program_day1__input_length = glGetUniformLocation(program_day1, "input_length");
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
// buffers
|
// buffers
|
||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
@ -118,6 +150,10 @@ int main()
|
|||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
|
int max_texture_size;
|
||||||
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
|
||||||
|
printf("max_texture_size %d\n", max_texture_size);
|
||||||
|
|
||||||
while(!glfwWindowShouldClose(window)) {
|
while(!glfwWindowShouldClose(window)) {
|
||||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
@ -134,12 +170,21 @@ int main()
|
|||||||
glBindTexture(GL_TEXTURE_2D, texture_input);
|
glBindTexture(GL_TEXTURE_2D, texture_input);
|
||||||
uint draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
|
uint draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
|
||||||
glDrawBuffers(1, draw_buffers);
|
glDrawBuffers(1, draw_buffers);
|
||||||
|
/*
|
||||||
glUseProgram(program_test);
|
glUseProgram(program_test);
|
||||||
glUniform1i(program_test__tex_sampler, 0);
|
glUniform1i(program_test__tex_sampler, 0);
|
||||||
glUniform4f(program_test__tex_size,
|
glUniform4f(program_test__tex_size,
|
||||||
input_width, input_height,
|
input_width, input_height,
|
||||||
0.5f / input_width,
|
0.5f / input_width,
|
||||||
0.5f / input_height);
|
0.5f / input_height);
|
||||||
|
*/
|
||||||
|
glUseProgram(program_day1);
|
||||||
|
glUniform1i(program_day1__tex_sampler, 0);
|
||||||
|
glUniform4f(program_day1__tex_size,
|
||||||
|
input_width, input_height,
|
||||||
|
0.5f / input_width,
|
||||||
|
0.5f / input_height);
|
||||||
|
glUniform1f(program_day1__input_length, input_length);
|
||||||
glBindVertexArray(vertex_array);
|
glBindVertexArray(vertex_array);
|
||||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||||
|
|
||||||
@ -160,14 +205,6 @@ int main()
|
|||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
int sum = 0;
|
|
||||||
for (int i = 0; i < buf_width * buf_height; i++) {
|
|
||||||
printf("%d ", data[i]);
|
|
||||||
if ((i % 4) == 3)
|
|
||||||
printf("\n");
|
|
||||||
sum += data[i];
|
|
||||||
}
|
|
||||||
printf("sum: %d\n", sum);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
85
src/shader/day1.fs.glsl
Normal file
85
src/shader/day1.fs.glsl
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
out vec4 fragment_color;
|
||||||
|
|
||||||
|
uniform sampler2D tex_sampler;
|
||||||
|
uniform vec4 tex_size; // w h halfpx_w halfpx_h
|
||||||
|
uniform float input_length;
|
||||||
|
|
||||||
|
vec2 rectangular_position(float ix)
|
||||||
|
{
|
||||||
|
float y = floor(ix * tex_size.z * 2.0);
|
||||||
|
float x = ix - tex_size.x * y;
|
||||||
|
|
||||||
|
return vec2((x * 2.0 + 1) * tex_size.z,
|
||||||
|
(y * 2.0 + 1) * tex_size.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
const float ascii_zero = 48.0;
|
||||||
|
const float ascii_nine = 57.0;
|
||||||
|
const float ascii_l = 76.0;
|
||||||
|
|
||||||
|
float get_input(float ix)
|
||||||
|
{
|
||||||
|
return texture(tex_sampler, rectangular_position(ix)).x * 255.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2 parse_integer(float ix)
|
||||||
|
{
|
||||||
|
float number = 0;
|
||||||
|
|
||||||
|
while (ix < input_length) {
|
||||||
|
float c = get_input(ix);
|
||||||
|
if (c < ascii_zero || c > ascii_nine) {
|
||||||
|
return vec2(ix + 1.0, number);
|
||||||
|
}
|
||||||
|
float digit = c - ascii_zero;
|
||||||
|
number = number * 10.0 + digit;
|
||||||
|
ix += 1.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2 parse_direction(float ix)
|
||||||
|
{
|
||||||
|
float c = get_input(ix);
|
||||||
|
float left = (c == ascii_l) ? 1.0 : 0.0;
|
||||||
|
return vec2(ix + 1.0, left);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2 simulate_movement(float ix, float position)
|
||||||
|
{
|
||||||
|
vec2 dir_result = parse_direction(ix);
|
||||||
|
ix = dir_result.x;
|
||||||
|
float left = dir_result.y;
|
||||||
|
|
||||||
|
vec2 int_result = parse_integer(ix);
|
||||||
|
ix = int_result.x;
|
||||||
|
float number = int_result.y;
|
||||||
|
|
||||||
|
if (left == 1.0) {
|
||||||
|
position = position - number;
|
||||||
|
} else {
|
||||||
|
position = position + number;
|
||||||
|
}
|
||||||
|
position = mod(position, 100);
|
||||||
|
return vec2(ix, position);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
float ix = 0.0;
|
||||||
|
float position = 50.0;
|
||||||
|
float zeros = 0.0;
|
||||||
|
|
||||||
|
while (ix < input_length) {
|
||||||
|
vec2 result = simulate_movement(ix, position);
|
||||||
|
ix = result.x;
|
||||||
|
position = result.y;
|
||||||
|
|
||||||
|
if (position == 0.0) {
|
||||||
|
zeros += 1.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment_color = vec4(position, zeros, 0, 0);
|
||||||
|
}
|
||||||
8
src/shader/day1.vs.glsl
Normal file
8
src/shader/day1.vs.glsl
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout (location = 0) in vec2 position;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(position, 0, 1);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user