Compare commits

..

No commits in common. "master" and "a512694d113090fd40f5f3e2121756be63480c44" have entirely different histories.

6 changed files with 30 additions and 76 deletions

View File

@ -1,15 +1,17 @@
const std = @import("std");
fn readHex(allocator: std.mem.Allocator, reader: anytype, max_size: usize) !?[]u8 {
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) {
const line = try reader.readUntilDelimiterOrEofAlloc(allocator, '\n', max_size * 2);
const hex = line orelse return null;
defer allocator.free(hex);
try resultbuffer.resize(hex.len / 2);
_ = std.fmt.hexToBytes(resultbuffer.items, hex) catch {
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;
};
@ -21,14 +23,13 @@ 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();
while (true) {
const input1 = (try readHex(allocator, stdin, 4096)) orelse break;
const input1 = try readHex(allocator, 4096);
defer allocator.free(input1);
const input2 = (try readHex(allocator, stdin, 4096)) orelse break;
const input2 = try readHex(allocator, 4096);
defer allocator.free(input2);
if (input1.len != input2.len) {

View File

@ -1,15 +1,17 @@
const std = @import("std");
fn readHex(allocator: std.mem.Allocator, reader: anytype, max_size: usize) !?[]u8 {
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) {
const line = try reader.readUntilDelimiterOrEofAlloc(allocator, '\n', max_size * 2);
const hex = line orelse return null;
defer allocator.free(hex);
try resultbuffer.resize(hex.len / 2);
_ = std.fmt.hexToBytes(resultbuffer.items, hex) catch {
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;
};
@ -40,11 +42,10 @@ 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();
while (true) {
const ciphertext = (try readHex(allocator, stdin, 4096)) orelse break;
const ciphertext = try readHex(allocator, 4096);
defer allocator.free(ciphertext);
var plaintext = try allocator.alloc(u8, ciphertext.len);
@ -52,7 +53,7 @@ pub fn main() anyerror!void {
var bestkey: u8 = 0;
var bestscore: u32 = 0;
var key: u8 = 0;
while (true) : (key += 1) {
while (true) {
decrypt(plaintext, ciphertext, key);
const s = score(plaintext);
if (s > bestscore) {
@ -60,7 +61,7 @@ pub fn main() anyerror!void {
bestscore = s;
}
key +%= 1;
if (key == std.math.maxInt(u8)) {
if (key == 0) {
break;
}
}

View File

@ -43,8 +43,8 @@ pub fn main() anyerror!void {
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
const stdout = std.io.getStdOut().writer();
var bestplaintext = std.ArrayList(u8).init(allocator);
defer bestplaintext.deinit();
var bestplaintext = try allocator.alloc(u8, 0);
defer allocator.free(bestplaintext);
var bestscore: u32 = 0;
while (true) {
@ -54,19 +54,21 @@ pub fn main() anyerror!void {
var plaintext = try allocator.alloc(u8, ciphertext.len);
defer allocator.free(plaintext);
var key: u8 = 0;
while (true) : (key += 1) {
while (true) {
decrypt(plaintext, ciphertext, key);
const s = score(plaintext);
if (s > bestscore) {
try bestplaintext.resize(plaintext.len);
std.mem.copy(u8, bestplaintext.items, plaintext);
allocator.free(bestplaintext);
bestplaintext = try allocator.alloc(u8, plaintext.len);
std.mem.copy(u8, bestplaintext, plaintext);
bestscore = s;
}
if (key == std.math.maxInt(u8)) {
key +%= 1;
if (key == 0) {
break;
}
}
}
try stdout.print("{s}\n", .{bestplaintext.items});
try stdout.print("{s}\n", .{bestplaintext});
}

View File

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

View File

@ -1,27 +0,0 @@
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

@ -1,21 +0,0 @@
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)});
}