|
|
@@ -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}));
|
|
|
+}
|
|
|
+
|