Compare commits
3 Commits
9f7f50260d
...
bae81315ad
Author | SHA1 | Date | |
---|---|---|---|
bae81315ad | |||
6b97979752 | |||
5cc36fc79f |
@ -9,10 +9,17 @@ int main() {
|
|||||||
ic_load_rom_from_input();
|
ic_load_rom_from_input();
|
||||||
|
|
||||||
IC *c = ic_new_computer();
|
IC *c = ic_new_computer();
|
||||||
ic_run(c);
|
c->input = 1;
|
||||||
|
//ic_run(c);
|
||||||
|
|
||||||
printf("Part 1: %d\n", c->output_buffer[c->output_buffer_count - 1]);
|
//printf("Part 1: %d\n", c->output_buffer[c->output_buffer_count - 1]);
|
||||||
// printf("Part 2: %d\n", noun * 100 + verb);
|
|
||||||
|
ic_reset(c);
|
||||||
|
ic_print(c);
|
||||||
|
c->input = 6;
|
||||||
|
ic_run(c);
|
||||||
|
ic_print(c);
|
||||||
|
printf("Part 2: %d\n", c->output_buffer[0]);
|
||||||
|
|
||||||
aoc_free();
|
aoc_free();
|
||||||
return 0;
|
return 0;
|
||||||
|
145
c/2024/17/chronospatial_computer.c
Normal file
145
c/2024/17/chronospatial_computer.c
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
#include "../../lib/aoc.h"
|
||||||
|
|
||||||
|
#define PROGRAM_LENGTH 16
|
||||||
|
|
||||||
|
typedef struct Computer {
|
||||||
|
long a;
|
||||||
|
long b;
|
||||||
|
long c;
|
||||||
|
long program[PROGRAM_LENGTH];
|
||||||
|
int pc;
|
||||||
|
long output[1000];
|
||||||
|
int output_count;
|
||||||
|
} Computer;
|
||||||
|
|
||||||
|
long combo(Computer *c, int operand) {
|
||||||
|
switch (operand) {
|
||||||
|
case 0:
|
||||||
|
case 1:
|
||||||
|
case 2:
|
||||||
|
case 3:
|
||||||
|
return operand;
|
||||||
|
case 4:
|
||||||
|
return c->a;
|
||||||
|
case 5:
|
||||||
|
return c->b;
|
||||||
|
case 6:
|
||||||
|
return c->c;
|
||||||
|
default:
|
||||||
|
printf("Invalid operand %d detected\n", operand);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void step(Computer *c) {
|
||||||
|
int instruction = c->program[c->pc++];
|
||||||
|
int operand = c->program[c->pc++];
|
||||||
|
// printf("%d|%d\n", instruction, operand);
|
||||||
|
// printf("a: %d, b: %d, c: %d, pc: %d\n", c->a, c->b, c->c, c->pc);
|
||||||
|
switch(instruction) {
|
||||||
|
case 0:
|
||||||
|
// printf("0: a = %d / %d = %d\n", c->a, 1<<combo(c, operand), c->a / (1<<combo(c, operand)));
|
||||||
|
c->a = c->a / (1 << combo(c, operand));
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
// printf("1: b = %d ^ %d = %d\n", c->b, operand, c->b ^ operand);
|
||||||
|
c->b = operand ^ c->b;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
// printf("2: b = %d % 8 = %d\n", combo(c, operand), combo(c, operand) % 8);
|
||||||
|
c->b = combo(c, operand) % 8;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
// printf("3: if %d then jump to %d\n", c->a, operand);
|
||||||
|
if (c->a) c->pc = operand;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
// printf("4: b = %d ^ %d = %d\n", c->b, c->c, c->b ^ c->c);
|
||||||
|
c->b = c->b ^ c->c;
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
// printf("5: print %d\n", combo(c, operand) % 8);
|
||||||
|
c->output[c->output_count++] = combo(c, operand) % 8;
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
// printf("6: b = %d / %d = %d\n", c->b, 1<<combo(c, operand), c->b / (1<<combo(c, operand)));
|
||||||
|
c->b = c->a / (1 << combo(c, operand));
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
// printf("7: c = %d / %d = %d\n", c->a, 1<<combo(c, operand), c->a / (1<<combo(c, operand)));
|
||||||
|
c->c = c->a / (1 << combo(c, operand));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool quine(Computer *c, long a) {
|
||||||
|
if (a % 100000000 == 0) printf("%ld\n", a);
|
||||||
|
c->a = a;
|
||||||
|
c->b = 0;
|
||||||
|
c->c = 0;
|
||||||
|
c->pc = 0;
|
||||||
|
c->output_count = 0;
|
||||||
|
while (c->pc < PROGRAM_LENGTH) {
|
||||||
|
step(c);
|
||||||
|
if (c->output_count == 0) continue;
|
||||||
|
if (c->output[c->output_count - 1] != c->program[c->output_count - 1]) return false;
|
||||||
|
}
|
||||||
|
if (c->output_count != PROGRAM_LENGTH) return false;
|
||||||
|
for (int i = 0; i < c->output_count; c++) {
|
||||||
|
if (c->program[i] != c->output[i]) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("%s\n", aoc_read_input());
|
||||||
|
Computer c;
|
||||||
|
c.pc = 0;
|
||||||
|
|
||||||
|
sscanf(aoc_read_line(), "Register A: %d", &(c.a));
|
||||||
|
sscanf(aoc_read_line(), "Register B: %d", &(c.b));
|
||||||
|
sscanf(aoc_read_line(), "Register C: %d", &(c.c));
|
||||||
|
aoc_read_line();
|
||||||
|
strtok(aoc_read_line(), " ");
|
||||||
|
for (char *token = strtok(NULL, ","); token != NULL; token = strtok(NULL, ",")) {
|
||||||
|
c.program[c.pc++] = atoi(token);
|
||||||
|
}
|
||||||
|
c.pc = 0;
|
||||||
|
c.output_count = 0;
|
||||||
|
|
||||||
|
while (c.pc < PROGRAM_LENGTH) {
|
||||||
|
printf("a: %d, b: %d, c: %d, pc: %d\n", c.a, c.b, c.c, c.pc);
|
||||||
|
step(&c);
|
||||||
|
// printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Part 1: %d", c.output[0]);
|
||||||
|
for (int i = 1; i < c.output_count; i++) {
|
||||||
|
printf(",%d", c.output[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
//long a = 100000000000000;
|
||||||
|
// long a = 8 ** 15;
|
||||||
|
// long a = 2 ** 45;
|
||||||
|
long a = 1l << 45l;
|
||||||
|
for (; a < (1l << 48l); a++) {
|
||||||
|
quine(&c, a);
|
||||||
|
/*printf("%ld: ", a);
|
||||||
|
for (int i = 0; i < c.output_count; i++) {
|
||||||
|
printf("%ld,", c.output[i]);
|
||||||
|
}
|
||||||
|
printf("\n");*/
|
||||||
|
}
|
||||||
|
// while (!quine(&c, a)) a++;
|
||||||
|
printf("Part 2: %ld", a);
|
||||||
|
|
||||||
|
aoc_free();
|
||||||
|
exit(0);
|
||||||
|
}
|
@ -15,6 +15,7 @@ typedef struct IC {
|
|||||||
int *data;
|
int *data;
|
||||||
int program_counter;
|
int program_counter;
|
||||||
bool halted;
|
bool halted;
|
||||||
|
int input;
|
||||||
int output_buffer[OUTPUT_BUFFER_LENGTH];
|
int output_buffer[OUTPUT_BUFFER_LENGTH];
|
||||||
int output_buffer_count;
|
int output_buffer_count;
|
||||||
} IC;
|
} IC;
|
||||||
@ -35,6 +36,10 @@ void ic_write(IC *c, int value) {
|
|||||||
ic_poke(c, ic_peek(c, c->program_counter++), value);
|
ic_poke(c, ic_peek(c, c->program_counter++), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ic_jump(IC *c, int address) {
|
||||||
|
c->program_counter = address;
|
||||||
|
}
|
||||||
|
|
||||||
void ic_halt(IC *c) {
|
void ic_halt(IC *c) {
|
||||||
c->halted = true;
|
c->halted = true;
|
||||||
}
|
}
|
||||||
@ -68,7 +73,7 @@ void ic_instruction_multiply(IC *c, int modes) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ic_instruction_input(IC *c, int _modes) {
|
void ic_instruction_input(IC *c, int _modes) {
|
||||||
ic_write(c, 1);
|
ic_write(c, c->input);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ic_instruction_output(IC *c, int modes) {
|
void ic_instruction_output(IC *c, int modes) {
|
||||||
@ -84,16 +89,92 @@ void ic_instruction_output(IC *c, int modes) {
|
|||||||
c->output_buffer[c->output_buffer_count++] = to_output;
|
c->output_buffer[c->output_buffer_count++] = to_output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ic_instruction_jump_if_true(IC *c, int modes) {
|
||||||
|
int predicate_mode = modes % 10;
|
||||||
|
modes /= 10;
|
||||||
|
int predicate = ic_read(c);
|
||||||
|
if (predicate_mode == POSITION_MODE) predicate = ic_peek(c, predicate);
|
||||||
|
printf("Predicate: %d\n", predicate);
|
||||||
|
if (predicate == 0) {
|
||||||
|
ic_read(c);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pointer_mode = 0;//modes % 10;
|
||||||
|
modes /= 10;
|
||||||
|
int pointer = ic_read(c);
|
||||||
|
if (pointer_mode == POSITION_MODE) pointer = ic_peek(c, pointer);
|
||||||
|
ic_jump(c, pointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ic_instruction_jump_if_false(IC *c, int modes) {
|
||||||
|
int predicate_mode = modes % 10;
|
||||||
|
modes /= 10;
|
||||||
|
int predicate = ic_read(c);
|
||||||
|
if (predicate_mode == POSITION_MODE) predicate = ic_peek(c, predicate);
|
||||||
|
if (predicate != 0) {
|
||||||
|
ic_read(c);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pointer_mode = 0;//modes % 10;
|
||||||
|
modes /= 10;
|
||||||
|
int pointer = ic_read(c);
|
||||||
|
if (pointer_mode == POSITION_MODE) pointer = ic_peek(c, pointer);
|
||||||
|
ic_jump(c, pointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ic_instruction_less_than(IC *c, int modes) {
|
||||||
|
int x_mode = modes % 10;
|
||||||
|
modes /= 10;
|
||||||
|
int x = ic_read(c);
|
||||||
|
if (x_mode == POSITION_MODE) x = ic_peek(c, x);
|
||||||
|
|
||||||
|
int y_mode = modes % 10;
|
||||||
|
modes /= 10;
|
||||||
|
int y = ic_read(c);
|
||||||
|
if (y_mode == POSITION_MODE) y = ic_peek(c, y);
|
||||||
|
|
||||||
|
int output_mode = 1;//modes % 10;
|
||||||
|
modes /= 10;
|
||||||
|
int output = ic_read(c);
|
||||||
|
if (output_mode == POSITION_MODE) output = ic_peek(c, output);
|
||||||
|
|
||||||
|
if (x < y) ic_poke(c, output, 1);
|
||||||
|
else ic_poke(c, output, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ic_instruction_equals(IC *c, int modes) {
|
||||||
|
int x_mode = modes % 10;
|
||||||
|
modes /= 10;
|
||||||
|
int x = ic_read(c);
|
||||||
|
if (x_mode == POSITION_MODE) x = ic_peek(c, x);
|
||||||
|
|
||||||
|
int y_mode = modes % 10;
|
||||||
|
modes /= 10;
|
||||||
|
int y = ic_read(c);
|
||||||
|
if (y_mode == POSITION_MODE) y = ic_peek(c, y);
|
||||||
|
|
||||||
|
int output_mode = 1;//modes % 10;
|
||||||
|
modes /= 10;
|
||||||
|
int output = ic_read(c);
|
||||||
|
if (output_mode == POSITION_MODE) output = ic_peek(c, output);
|
||||||
|
|
||||||
|
if (x == y) ic_poke(c, output, 1);
|
||||||
|
else ic_poke(c, output, 0);
|
||||||
|
}
|
||||||
|
|
||||||
void ic_instruction_halt(IC *c, int _modes) {
|
void ic_instruction_halt(IC *c, int _modes) {
|
||||||
ic_halt(c);
|
ic_halt(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ic_execute_instruction(IC *c) {
|
int ic_execute_instruction(IC *c) {
|
||||||
|
//ic_print(c);
|
||||||
int instruction = ic_read(c);
|
int instruction = ic_read(c);
|
||||||
int opcode = instruction % 100;
|
int opcode = instruction % 100;
|
||||||
int modes = instruction / 100;
|
int modes = instruction / 100;
|
||||||
// printf("Running %d: opcode %d with modes %d\n", instruction, opcode, modes);
|
printf("Running %d: opcode %d with modes %d\n", instruction, opcode, modes);
|
||||||
// printf("PC: %d\n", c->program_counter - 1);
|
printf("PC: %d\n", c->program_counter - 1);
|
||||||
switch(opcode) {
|
switch(opcode) {
|
||||||
case 1:
|
case 1:
|
||||||
ic_instruction_add(c, modes);
|
ic_instruction_add(c, modes);
|
||||||
@ -107,6 +188,18 @@ int ic_execute_instruction(IC *c) {
|
|||||||
case 4:
|
case 4:
|
||||||
ic_instruction_output(c, modes);
|
ic_instruction_output(c, modes);
|
||||||
break;
|
break;
|
||||||
|
case 5:
|
||||||
|
ic_instruction_jump_if_true(c, modes);
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
ic_instruction_jump_if_false(c, modes);
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
ic_instruction_less_than(c, modes);
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
ic_instruction_equals(c, modes);
|
||||||
|
break;
|
||||||
case 99:
|
case 99:
|
||||||
ic_instruction_halt(c, modes);
|
ic_instruction_halt(c, modes);
|
||||||
break;
|
break;
|
||||||
|
3
c_visualization/.gitignore
vendored
Normal file
3
c_visualization/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
lib/*.a
|
||||||
|
|
||||||
|
*/*/problem
|
70
c_visualization/2015/1/not_quite_lisp.c
Normal file
70
c_visualization/2015/1/not_quite_lisp.c
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
/*******************************************************************************************
|
||||||
|
*
|
||||||
|
* raylib [core] example - Basic window
|
||||||
|
*
|
||||||
|
* Welcome to raylib!
|
||||||
|
*
|
||||||
|
* To test examples, just press F6 and execute 'raylib_compile_execute' script
|
||||||
|
* Note that compiled executable is placed in the same folder as .c file
|
||||||
|
*
|
||||||
|
* To test the examples on Web, press F6 and execute 'raylib_compile_execute_web' script
|
||||||
|
* Web version of the program is generated in the same folder as .c file
|
||||||
|
*
|
||||||
|
* You can find all basic examples on C:\raylib\raylib\examples folder or
|
||||||
|
* raylib official webpage: www.raylib.com
|
||||||
|
*
|
||||||
|
* Enjoy using raylib. :)
|
||||||
|
*
|
||||||
|
* Example originally created with raylib 1.0, last time updated with raylib 1.0
|
||||||
|
*
|
||||||
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||||
|
* BSD-like license that allows static linking with closed source software
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
********************************************************************************************/
|
||||||
|
|
||||||
|
#include <raylib.h>
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Program main entry point
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
const int screenWidth = 800;
|
||||||
|
const int screenHeight = 450;
|
||||||
|
|
||||||
|
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
|
||||||
|
|
||||||
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// TODO: Update your variables here
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
BeginDrawing();
|
||||||
|
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
|
||||||
|
|
||||||
|
EndDrawing();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
|
||||||
|
// De-Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
15
c_visualization/bin/run
Executable file
15
c_visualization/bin/run
Executable file
@ -0,0 +1,15 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
year=$1
|
||||||
|
day=$2
|
||||||
|
|
||||||
|
mkdir -p $year/$day
|
||||||
|
source_file=$(ls $year/$day/*.c)
|
||||||
|
|
||||||
|
if [[ -z $source_file ]] ; then
|
||||||
|
echo "No c source file found in $year/$day"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
gcc -Wall -o $year/$day/problem $source_file -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 &&
|
||||||
|
time (cat ../data/$year/$day/input.txt | ./$year/$day/problem)
|
Loading…
Reference in New Issue
Block a user