Skip to content

Commit

Permalink
2019 day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
agagniere committed Dec 5, 2024
1 parent db32c34 commit be2b3fb
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 3 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
37 changes: 37 additions & 0 deletions 2019/05/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const libft = b.dependency("libft", .{ .target = target, .optimize = optimize }).artifact("ft");

const exe = b.addExecutable(.{
.name = "intcode",
.target = target,
.optimize = optimize,
});
exe.linkLibC();
exe.addCSourceFile(.{ .file = b.path("solve.c"), .flags = &CFLAGS });
exe.linkLibrary(libft);
b.installArtifact(exe);

{ // Run
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe);

run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
run_step.dependOn(&run_cmd.step);
}
}

const CFLAGS = .{
"-Werror",
"-Wall",
"-Wextra",
"-Wswitch",
"-Wvla",
};
11 changes: 11 additions & 0 deletions 2019/05/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.{
.name = "intcode",
.version = "0.0.5",
.dependencies = .{
.libft = .{
.url = "git+https://github.com/agagniere/Libft#03e1669da64f9dbb0aa03f1f46e14efe4353ab51",
.hash = "122068f7a39abb7b4baab50d424fb490f757ceefc414b60d2d34be3c1b0c85b69641",
},
},
.paths = .{ "build.zig", "build.zig.zon", "intcode.c" },
}
128 changes: 128 additions & 0 deletions 2019/05/solve.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include <libft/ft_array.h> // t_array

#include <stdint.h> // int64_t
#include <stdio.h> // printf
#include <stdlib.h> // EXIT_SUCCESS
#include <stdbool.h> // bool true false
#include <string.h> // strcmp
#include <inttypes.h> // PRI*

#ifdef NDEBUG
# define DEBUG(A)
#else
# define DEBUG(A) A
#endif

enum Instruction {
Add = 1,
Multiply,
Input,
Output,
JumpIfTrue,
JumpIfFalse,
LessThan,
Equals,
Stop = 99
};

/**
* Interpret @p program as intcode
* @param input: values used by the input opcode, used as a stack (reverse order)
*/
int64_t intcode(int64_t* program, t_array* input)
{
int64_t *p = program;

while (true)
{
bool mode1 = (*p / 100) % 2;
bool mode2 = (*p / 1000) % 2;
enum Instruction instruction = *p % 100;

switch (instruction)
{
case Add:
program[p[3]] = (mode1 ? p[1] : program[p[1]]) + (mode2 ? p[2] : program[p[2]]);
p += 4;
break;
case Multiply:
program[p[3]] = (mode1 ? p[1] : program[p[1]]) * (mode2 ? p[2] : program[p[2]]);
p += 4;
break;
case Input:
DEBUG(if (input->size == 0) {fprintf(stderr, "Out of input\n"); return -1;});
program[p[1]] = *(int64_t*)ARRAY_LAST(input);
DEBUG(printf("Read: %" PRIi64 "\n", program[p[1]]));
fta_popback(input, 1);
p += 2;
break;
case Output:
printf("%" PRIi64 "\n", mode1 ? p[1] : program[p[1]]);
p += 2;
break;
case JumpIfTrue:
if (mode1 ? p[1] : program[p[1]])
p = program + (mode2 ? p[2] : program[p[2]]);
else
p += 3;
break;
case JumpIfFalse:
if (!(mode1 ? p[1] : program[p[1]]))
p = program + (mode2 ? p[2] : program[p[2]]);
else
p += 3;
break;
case LessThan:
program[p[3]] = (mode1 ? p[1] : program[p[1]]) < (mode2 ? p[2] : program[p[2]]);
p += 4;
break;
case Equals:
program[p[3]] = (mode1 ? p[1] : program[p[1]]) == (mode2 ? p[2] : program[p[2]]);
p += 4;
break;
case Stop:
return *program;
default:
fprintf(stderr, "Unsupported instruction: %u\n", instruction);
return -1;
}
}
return *program;
}

size_t read_stdin(int64_t** program)
{
t_array result = NEW_ARRAY(int64_t);
char* buffer = NULL;
size_t buffer_length = 0;
ssize_t status;
int64_t n;

while ((status = getdelim(&buffer, &buffer_length, ',', stdin)) > 0)
{
n = atoi(buffer);
fta_append(&result, &n, 1);
}
free(buffer);
*program = result.data;
return result.size;
}

int main(int ac, char** av)
{
int64_t* program;
t_array input = NEW_ARRAY(int64_t);
int64_t n;

read_stdin(&program);
fta_reserve(&input, ac);
while (ac --> 1)
{
n = atoi(av[ac]);
fta_append(&input, &n, 1);
}
intcode(program, &input);
free(program);
fta_clear(&input);
return EXIT_SUCCESS;
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ My solutions for the [Advent of Code](https://adventofcode.com), a challenge sta
| 02 |:star::star:|:star::star:|:star::star:|:star::star:|:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 03 |:star::star:|:star::star:||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 04 |:star::star:|:star::star:||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 05 |:star::star:|:star::star:|||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 05 |:star::star:|:star::star:||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 06 ||:star::star:|||:star::star:|:star::star:|:star::star:|:star::star:|
| 07 ||:star::star:|||:star::star:|:star::star:|:star::star:|:star::star:|
| 08 |||||:star::star:|:star::star:|:star::star:|:star::star:|
Expand All @@ -30,6 +30,6 @@ My solutions for the [Advent of Code](https://adventofcode.com), a challenge sta
| 23 |||||||:star::star:|:star:|
| 24 ||||||:star::star:|:star::star:|:star:|
| 25 |||||:star:|:star:|:star::star:||
| Total | 10 | 14 | 4 | 8 | 42 | 44 | 50 | 44 | 10
| Total | 10 | 14 | 4 | 10 | 42 | 44 | 50 | 44 | 10

Total stars: 226
Total stars: 228

0 comments on commit be2b3fb

Please sign in to comment.