| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- const std = @import("std");
- const allocator = std.heap.page_allocator;
- pub fn main() !void {
- var printBuf: [1024]u8 = undefined;
- 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();
- const inputBuffer = try allocator.alloc(u8, inputFileSize);
- try inputFileReader.interface.readSliceAll(inputBuffer);
- var counter:u64 = 0;
- var it = std.mem.tokenizeAny(u8, inputBuffer, ",\n");
- while (it.next()) |token| {
- const dashPos = std.mem.indexOf(u8, token, "-").?;
- const startRangeText = token[0..dashPos];
- const endRangeText = token[dashPos+1..];
- const startRange = try std.fmt.parseInt(u64, startRangeText, 10);
- const endRange = try std.fmt.parseInt(u64, endRangeText, 10);
- for (startRange..endRange+1) |id| {
- const idText = try std.fmt.bufPrint(&printBuf, "{d}", .{id});
- const halfLen = idText.len/2;
- const firstHalfText = idText[0..halfLen];
- const secondHalfText = idText[halfLen..];
- if (std.mem.eql(u8, firstHalfText, secondHalfText)) {
- counter += id;
- }
- }
- }
- try std.fs.File.stdout().writeAll(
- try std.fmt.bufPrint(&printBuf, "{}\n", .{counter}));
- }
|