solve Challenge 5 of Set 1

This commit is contained in:
Thomas Lindner 2022-02-07 01:06:53 +01:00
parent 6abec1ca62
commit e412d63914
3 changed files with 50 additions and 0 deletions

View file

@ -0,0 +1,2 @@
Burning 'em, if you ain't quick and nimble
I go crazy when I hear a cymbal

View file

@ -0,0 +1,27 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("challenge5_repeating_xor", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}

View file

@ -0,0 +1,21 @@
const std = @import("std");
pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
const stdout = std.io.getStdOut().writer();
const key = "ICE";
const input = try stdin.readAllAlloc(allocator, 4096);
defer allocator.free(input);
var i: usize = 0;
for (input) |*char| {
char.* ^= key[i];
i = (i + 1) % key.len;
}
try stdout.print("{s}\n", .{std.fmt.fmtSliceHexLower(input)});
}