[TASK] Finally finished day 1 with very ugly zig code xD
This commit is contained in:
4
day1-zig/input/day1_example2.txt
Normal file
4
day1-zig/input/day1_example2.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
L1000
|
||||
L50
|
||||
R50
|
||||
R50
|
||||
@@ -3,37 +3,109 @@
|
||||
//! is to delete this file and start with root.zig instead.
|
||||
|
||||
pub fn main() !void {
|
||||
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
||||
const part1Solution = try part1("input/day1.txt");
|
||||
std.debug.print("Part1:{}\n", .{part1Solution});
|
||||
|
||||
// stdout is for the actual output of your application, for example if you
|
||||
// are implementing gzip, then only the compressed bytes should be sent to
|
||||
// stdout, not any debugging messages.
|
||||
const stdout_file = std.io.getStdOut().writer();
|
||||
var bw = std.io.bufferedWriter(stdout_file);
|
||||
const stdout = bw.writer();
|
||||
|
||||
try stdout.print("Run `zig build test` to run the tests.\n", .{});
|
||||
|
||||
try bw.flush(); // Don't forget to flush!
|
||||
const part2Solution = try part2("input/day1.txt");
|
||||
std.debug.print("Part2:{}\n", .{part2Solution});
|
||||
}
|
||||
|
||||
fn parse(input)
|
||||
fn part1(path: []const u8) !i32 {
|
||||
var file = try std.fs.cwd().openFile(path, .{});
|
||||
defer file.close();
|
||||
|
||||
test "Part 1 sample" {
|
||||
const input = try std.fs.cwd().readFile("day1_example.txt", .{});
|
||||
var buf_reader = std.io.bufferedReader(file.reader());
|
||||
var in_stream = buf_reader.reader();
|
||||
|
||||
}
|
||||
var pos: i32 = 50;
|
||||
var count: i32 = 0;
|
||||
|
||||
test "fuzz example" {
|
||||
const Context = struct {
|
||||
fn testOne(context: @This(), input: []const u8) anyerror!void {
|
||||
_ = context;
|
||||
// Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
|
||||
try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
|
||||
var buf: [100]u8 = undefined;
|
||||
while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
|
||||
const shift = try std.fmt.parseInt(i32, line[1..], 10);
|
||||
if (line[0] == 'L') {
|
||||
pos -= shift;
|
||||
while (pos < 0) {
|
||||
pos += 100;
|
||||
}
|
||||
};
|
||||
try std.testing.fuzz(Context{}, Context.testOne, .{});
|
||||
} else {
|
||||
pos += shift;
|
||||
pos = @mod(pos, 100);
|
||||
}
|
||||
if (pos == 0) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
fn part2(path: []const u8) !i32 {
|
||||
var file = try std.fs.cwd().openFile(path, .{});
|
||||
defer file.close();
|
||||
|
||||
var buf_reader = std.io.bufferedReader(file.reader());
|
||||
var in_stream = buf_reader.reader();
|
||||
|
||||
var pos: i32 = 50;
|
||||
var count: i32 = 0;
|
||||
|
||||
var buf: [100]u8 = undefined;
|
||||
while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
|
||||
var shift = try std.fmt.parseInt(i32, line[1..], 10);
|
||||
if (shift > 100) {
|
||||
count += @divFloor(shift, 100);
|
||||
shift = @mod(shift, 100);
|
||||
}
|
||||
const startPos = pos;
|
||||
if (line[0] == 'L') {
|
||||
pos -= shift;
|
||||
if (pos < 0) {
|
||||
pos += 100;
|
||||
if (startPos != 0) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pos += shift;
|
||||
if (pos > 100) {
|
||||
count += 1;
|
||||
pos = @mod(pos, 100);
|
||||
} else if (pos == 100) {
|
||||
pos = 0;
|
||||
}
|
||||
}
|
||||
if (pos == 0) {
|
||||
count += 1;
|
||||
}
|
||||
std.debug.print("pos: {}, count: {}\n", .{ pos, count });
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
test "Part 1 example" {
|
||||
const res = try part1("input/day1_example.txt");
|
||||
try std.testing.expect(res == 3);
|
||||
}
|
||||
|
||||
test "Part 2 example" {
|
||||
const res = try part2("input/day1_example.txt");
|
||||
try std.testing.expect(res == 6);
|
||||
}
|
||||
|
||||
test "Part 2 example 2" {
|
||||
const res = try part2("input/day1_example2.txt");
|
||||
try std.testing.expect(res == 12);
|
||||
}
|
||||
|
||||
// test "fuzz example" {
|
||||
// const Context = struct {
|
||||
// fn testOne(context: @This(), input: []const u8) anyerror!void {
|
||||
// _ = context;
|
||||
// // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
|
||||
// try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
|
||||
// }
|
||||
// };
|
||||
// try std.testing.fuzz(Context{}, Context.testOne, .{});
|
||||
// }
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
Reference in New Issue
Block a user