| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- 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}));
- }
|