day12a.zig 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const std = @import("std");
  2. var allocatorBuffer: [100*1024*1024]u8 = undefined;
  3. const Shape = struct {
  4. data: [9]u8,
  5. fn getCell(self: *const Shape, row: u8, col: u8) u8 {
  6. return self.data[row*3 + col];
  7. }
  8. fn setCell(self: *Shape, row: u8, col: u8, cell: u8) void {
  9. self.data[row*3 + col] = cell;
  10. }
  11. };
  12. const Area = struct {
  13. rows: u8,
  14. cols: u8,
  15. shapesCount: []u8,
  16. };
  17. const Input = struct {
  18. shapes: []Shape,
  19. areas: []Area,
  20. };
  21. pub fn parseShape(shapeText: []const u8) Shape {
  22. var it = std.mem.tokenizeScalar(u8, shapeText, '\n');
  23. _ = it.next(); // skip header
  24. var shape: Shape = .{
  25. .data = undefined,
  26. };
  27. for (0..3) |row| {
  28. const rowText = it.next().?;
  29. for (0..3) |col| {
  30. shape.setCell(@intCast(row), @intCast(col), rowText[col]);
  31. }
  32. }
  33. return shape;
  34. }
  35. pub fn parseArea(allocator: std.mem.Allocator, line: []const u8) !Area {
  36. var it = std.mem.tokenizeSequence(u8, line, ": ");
  37. const sizeText = it.next().?;
  38. const shapesCountText = it.next().?;
  39. var sizeTextIt = std.mem.tokenizeScalar(u8, sizeText, 'x');
  40. const rowsText = sizeTextIt.next().?;
  41. const colsText = sizeTextIt.next().?;
  42. const rows = try std.fmt.parseInt(u8, rowsText, 10);
  43. const cols = try std.fmt.parseInt(u8, colsText, 10);
  44. var shapesCountTextIt = std.mem.tokenizeScalar(u8, shapesCountText, ' ');
  45. var shapesCount = std.ArrayList(u8).empty;
  46. while (shapesCountTextIt.next()) |shapeCountText| {
  47. const shapeCount = try shapesCount.addOne(allocator);
  48. shapeCount.* = try std.fmt.parseInt(u8, shapeCountText, 10);
  49. }
  50. const area: Area = .{
  51. .rows = rows,
  52. .cols = cols,
  53. .shapesCount = shapesCount.items,
  54. };
  55. return area;
  56. }
  57. pub fn parseInput(allocator: std.mem.Allocator, inputBuffer: []u8) !Input {
  58. var shapes = std.ArrayList(Shape).empty;
  59. var areas = std.ArrayList(Area).empty;
  60. var i = std.mem.tokenizeSequence(u8, inputBuffer, "\n\n");
  61. while (i.next()) |inputBlock| {
  62. if (inputBlock.len >= 2 and inputBlock[1] == ':') {
  63. const shape = try shapes.addOne(allocator);
  64. shape.* = parseShape(inputBlock);
  65. } else {
  66. var it = std.mem.tokenizeScalar(u8, inputBlock, '\n');
  67. while(it.next()) |line| {
  68. const area = try areas.addOne(allocator);
  69. area.* = try parseArea(allocator, line);
  70. }
  71. }
  72. }
  73. const input: Input = .{
  74. .shapes = shapes.items,
  75. .areas = areas.items,
  76. };
  77. return input;
  78. }
  79. pub fn readInputIntoString(allocator: std.mem.Allocator, fileName: []const u8) ![]u8 {
  80. const inputFile = try std.fs.cwd().openFile(
  81. fileName, .{.mode = .read_only,});
  82. defer inputFile.close();
  83. var inputFileReader = inputFile.reader(
  84. try allocator.alignedAlloc(u8, null, 1024));
  85. const inputFileSize = try inputFileReader.getSize();
  86. const inputBuffer = try allocator.alignedAlloc(u8, null, inputFileSize);
  87. try inputFileReader.interface.readSliceAll(inputBuffer);
  88. return inputBuffer;
  89. }
  90. pub fn main() !void {
  91. var fb_alloc = std.heap.FixedBufferAllocator.init(&allocatorBuffer);
  92. const allocator = fb_alloc.allocator();
  93. const inputBuffer = try readInputIntoString(allocator, "input.txt");
  94. const input = try parseInput(allocator, inputBuffer);
  95. for (input.areas) |area| {
  96. try std.fs.File.stdout().writeAll(
  97. try std.fmt.allocPrint(allocator, "area: rows={d}, cols={d}, shapes=[ ", .{area.rows, area.cols}));
  98. for (area.shapesCount) |shapeCount| {
  99. try std.fs.File.stdout().writeAll(
  100. try std.fmt.allocPrint(allocator, "{d}, ", .{shapeCount}));
  101. }
  102. try std.fs.File.stdout().writeAll("]\n");
  103. }
  104. for (input.shapes) |shape| {
  105. try std.fs.File.stdout().writeAll("shape:\n");
  106. for (0..3) |row| {
  107. for (0..3) |col| {
  108. const cell = shape.getCell(@intCast(row), @intCast(col));
  109. try std.fs.File.stdout().writeAll(
  110. try std.fmt.allocPrint(allocator, "{c}", .{cell}));
  111. }
  112. try std.fs.File.stdout().writeAll("\n");
  113. }
  114. try std.fs.File.stdout().writeAll("\n");
  115. }
  116. }