소스 검색

solved day2

Aleksei Dorokhov 7 달 전
부모
커밋
ffc4478122
4개의 변경된 파일141개의 추가작업 그리고 0개의 파일을 삭제
  1. 3 0
      day2/.gitignore
  2. 23 0
      day2/build.zig
  3. 48 0
      day2/day2a.zig
  4. 67 0
      day2/day2b.zig

+ 3 - 0
day2/.gitignore

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

+ 23 - 0
day2/build.zig

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

+ 48 - 0
day2/day2a.zig

@@ -0,0 +1,48 @@
+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}));
+}
+

+ 67 - 0
day2/day2b.zig

@@ -0,0 +1,67 @@
+const std = @import("std");
+
+const allocator = std.heap.page_allocator;
+
+fn isRepeatedSequence(sequence: []const u8, prefix: []const u8) !bool {
+    if (sequence.len == 0) {
+        return true;
+    }
+
+    if (!std.mem.startsWith(u8, sequence, prefix)) {
+        return false;
+    }
+
+    return isRepeatedSequence(sequence[prefix.len..], prefix);
+}
+
+fn isSillyId(idText: []const u8) !bool {
+    for (1..idText.len/2+1) |prefixLen| {
+        if (try isRepeatedSequence(idText[prefixLen..], idText[0..prefixLen])) {
+            return true;
+        }
+    }
+
+    return false;
+}
+
+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});
+            if (try isSillyId(idText)) {
+                counter += id;
+            }
+        }
+    }
+
+    try std.fs.File.stdout().writeAll(
+      try std.fmt.bufPrint(&printBuf, "{}\n", .{counter}));
+}
+