Aleksei Dorokhov 7 mesiacov pred
commit
a2b595a11f
5 zmenil súbory, kde vykonal 153 pridanie a 0 odobranie
  1. 3 0
      day1/.gitignore
  2. 23 0
      day1/build.zig
  3. 61 0
      day1/day1a.zig
  4. 61 0
      day1/day1b.zig
  5. 5 0
      day1/hello.zig

+ 3 - 0
day1/.gitignore

@@ -0,0 +1,3 @@
+input.txt
+.zig-cache
+zig-out

+ 23 - 0
day1/build.zig

@@ -0,0 +1,23 @@
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+    const exe1 = b.addExecutable(.{
+        .name = "day1a",
+        .root_module = b.createModule(.{
+            .root_source_file = b.path("day1a.zig"),
+            .target = b.graph.host,
+        }),
+    });
+
+    b.installArtifact(exe1);
+
+    const exe2 = b.addExecutable(.{
+        .name = "day1b",
+        .root_module = b.createModule(.{
+            .root_source_file = b.path("day1b.zig"),
+            .target = b.graph.host,
+        }),
+    });
+
+    b.installArtifact(exe2);
+}

+ 61 - 0
day1/day1a.zig

@@ -0,0 +1,61 @@
+const std = @import("std");
+
+const allocator = std.heap.page_allocator;
+
+const Dial = struct {
+    v: i32 = 50,
+
+    fn Left(self: *Dial, count: u32) void {
+        self.v -= @intCast(count % 100);
+        if (self.v < 0) {
+          self.v += 100;
+        } 
+    }
+
+    fn Right(self: *Dial, count: u32) void {
+        self.v += @intCast(count % 100);
+        if (self.v >= 100) {
+          self.v -= 100;
+        } 
+    }
+};
+
+pub fn main() !void {
+    const inputFile = try std.fs.cwd().openFile(
+        "input.txt",
+        .{
+            .mode = .read_only,
+        },
+    );
+    defer inputFile.close();
+
+    var inputFileBuffer: [1024]u8 = undefined;
+    var inputFileReader = inputFile.reader(&inputFileBuffer);
+    const inputFileSize = try inputFileReader.getSize();
+    var printBuf: [1024]u8 = undefined;
+
+    const inputBuffer = try allocator.alloc(u8, inputFileSize);
+    try inputFileReader.interface.readSliceAll(inputBuffer);
+
+    var dial: Dial = .{};
+    var zeroCount: i32 = 0;
+
+    var it = std.mem.tokenizeScalar(u8, inputBuffer, '\n');
+    while (it.next()) |token| {
+        const direction = token[0];
+        const count = try std.fmt.parseInt(u32, token[1..], 10);
+
+        if (direction == 'L') {
+            dial.Left(count);
+        } else {
+            dial.Right(count);
+        }
+
+        if (dial.v == 0) {
+            zeroCount += 1;
+        }
+    }
+
+    try std.fs.File.stdout().writeAll(try std.fmt.bufPrint(&printBuf, "{d}\n", .{zeroCount}));
+}
+

+ 61 - 0
day1/day1b.zig

@@ -0,0 +1,61 @@
+const std = @import("std");
+
+const allocator = std.heap.page_allocator;
+
+const Dial = struct {
+    v: i32 = 50,
+    c: i32 = 0,
+
+    fn Left(self: *Dial, count: u32) void {
+        self.c += @intCast(count / 100);
+        self.v -= @intCast(count % 100);
+        if (self.v < 0) {
+          self.c += 1;
+          self.v += 100;
+        } 
+    }
+
+    fn Right(self: *Dial, count: u32) void {
+        self.c += @intCast(count / 100);
+        self.v += @intCast(count % 100);
+        if (self.v >= 100) {
+          self.c += 1;
+          self.v -= 100;
+        } 
+    }
+};
+
+pub fn main() !void {
+    const inputFile = try std.fs.cwd().openFile(
+        "input.txt",
+        .{
+            .mode = .read_only,
+        },
+    );
+    defer inputFile.close();
+
+    var inputFileBuffer: [1024]u8 = undefined;
+    var inputFileReader = inputFile.reader(&inputFileBuffer);
+    const inputFileSize = try inputFileReader.getSize();
+    var printBuf: [1024]u8 = undefined;
+
+    const inputBuffer = try allocator.alloc(u8, inputFileSize);
+    try inputFileReader.interface.readSliceAll(inputBuffer);
+
+    var dial: Dial = .{};
+
+    var it = std.mem.tokenizeScalar(u8, inputBuffer, '\n');
+    while (it.next()) |token| {
+        const direction = token[0];
+        const count = try std.fmt.parseInt(u32, token[1..], 10);
+
+        if (direction == 'L') {
+            dial.Left(count);
+        } else {
+            dial.Right(count);
+        }
+    }
+
+    try std.fs.File.stdout().writeAll(try std.fmt.bufPrint(&printBuf, "{d}\n", .{dial.c}));
+}
+

+ 5 - 0
day1/hello.zig

@@ -0,0 +1,5 @@
+const std = @import("std");
+
+pub fn main() !void {
+    try std.fs.File.stdout().writeAll("Hello, World!!!\n");
+}