[TASK] Finally finished day 1 with very ugly zig code xD

This commit is contained in:
2025-12-05 00:06:15 +01:00
parent f24250bcc8
commit 5f4869705c
2 changed files with 101 additions and 25 deletions

View File

@@ -0,0 +1,4 @@
L1000
L50
R50
R50

View File

@@ -3,37 +3,109 @@
//! is to delete this file and start with root.zig instead. //! is to delete this file and start with root.zig instead.
pub fn main() !void { pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) const part1Solution = try part1("input/day1.txt");
std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); std.debug.print("Part1:{}\n", .{part1Solution});
// stdout is for the actual output of your application, for example if you const part2Solution = try part2("input/day1.txt");
// are implementing gzip, then only the compressed bytes should be sent to std.debug.print("Part2:{}\n", .{part2Solution});
// 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!
} }
fn parse(input) fn part1(path: []const u8) !i32 {
var file = try std.fs.cwd().openFile(path, .{});
defer file.close();
test "Part 1 sample" { var buf_reader = std.io.bufferedReader(file.reader());
const input = try std.fs.cwd().readFile("day1_example.txt", .{}); var in_stream = buf_reader.reader();
}
test "fuzz example" { var pos: i32 = 50;
const Context = struct { var count: i32 = 0;
fn testOne(context: @This(), input: []const u8) anyerror!void {
_ = context; var buf: [100]u8 = undefined;
// Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case! while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input)); const shift = try std.fmt.parseInt(i32, line[1..], 10);
if (line[0] == 'L') {
pos -= shift;
while (pos < 0) {
pos += 100;
}
} else {
pos += shift;
pos = @mod(pos, 100);
} }
}; if (pos == 0) {
try std.testing.fuzz(Context{}, Context.testOne, .{}); 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"); const std = @import("std");