solve Challenge 2 of Set 1
This commit is contained in:
parent
f042ca64b5
commit
19c48f9d58
27
set1/challenge2_fixed_xor/build.zig
Normal file
27
set1/challenge2_fixed_xor/build.zig
Normal 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("challenge2_fixed_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);
|
||||
}
|
50
set1/challenge2_fixed_xor/src/main.zig
Normal file
50
set1/challenge2_fixed_xor/src/main.zig
Normal file
|
@ -0,0 +1,50 @@
|
|||
const std = @import("std");
|
||||
|
||||
fn readHex(allocator: std.mem.Allocator, max_size: usize) ![]u8 {
|
||||
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
|
||||
|
||||
var inputbuffer = std.ArrayList(u8).init(allocator);
|
||||
defer inputbuffer.deinit();
|
||||
var resultbuffer = std.ArrayList(u8).init(allocator);
|
||||
defer resultbuffer.deinit();
|
||||
|
||||
while (true) {
|
||||
try stdin.readUntilDelimiterArrayList(&inputbuffer, '\n', max_size * 2);
|
||||
try resultbuffer.resize(inputbuffer.items.len / 2);
|
||||
_ = std.fmt.hexToBytes(resultbuffer.items, inputbuffer.items) catch {
|
||||
std.log.err("not a valid hexadecimal", .{});
|
||||
continue;
|
||||
};
|
||||
return resultbuffer.toOwnedSlice();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
while (true) {
|
||||
const input1 = try readHex(allocator, 4096);
|
||||
defer allocator.free(input1);
|
||||
|
||||
const input2 = try readHex(allocator, 4096);
|
||||
defer allocator.free(input2);
|
||||
|
||||
if (input1.len != input2.len) {
|
||||
std.log.err("not the same length", .{});
|
||||
continue;
|
||||
}
|
||||
|
||||
var output = try std.ArrayList(u8).initCapacity(allocator, input1.len);
|
||||
defer output.deinit();
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < input1.len) : (i += 1) {
|
||||
try output.append(input1[i] ^ input2[i]);
|
||||
}
|
||||
|
||||
try stdout.print("{}\n", .{std.fmt.fmtSliceHexLower(output.items)});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue