-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters