| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- const std = @import("std");
- var allocatorBuffer: [100*1024*1024]u8 = undefined;
- const Shape = struct {
- data: [9]u8,
- fn getCell(self: *const Shape, row: u8, col: u8) u8 {
- return self.data[row*3 + col];
- }
- fn setCell(self: *Shape, row: u8, col: u8, cell: u8) void {
- self.data[row*3 + col] = cell;
- }
- };
- const Area = struct {
- rows: u8,
- cols: u8,
- shapesCount: []u8,
- };
- const Input = struct {
- shapes: []Shape,
- areas: []Area,
- };
- pub fn parseShape(shapeText: []const u8) Shape {
- var it = std.mem.tokenizeScalar(u8, shapeText, '\n');
- _ = it.next(); // skip header
-
- var shape: Shape = .{
- .data = undefined,
- };
-
- for (0..3) |row| {
- const rowText = it.next().?;
- for (0..3) |col| {
- shape.setCell(@intCast(row), @intCast(col), rowText[col]);
- }
- }
- return shape;
- }
- pub fn parseArea(allocator: std.mem.Allocator, line: []const u8) !Area {
- var it = std.mem.tokenizeSequence(u8, line, ": ");
- const sizeText = it.next().?;
- const shapesCountText = it.next().?;
- var sizeTextIt = std.mem.tokenizeScalar(u8, sizeText, 'x');
- const rowsText = sizeTextIt.next().?;
- const colsText = sizeTextIt.next().?;
- const rows = try std.fmt.parseInt(u8, rowsText, 10);
- const cols = try std.fmt.parseInt(u8, colsText, 10);
- var shapesCountTextIt = std.mem.tokenizeScalar(u8, shapesCountText, ' ');
- var shapesCount = std.ArrayList(u8).empty;
- while (shapesCountTextIt.next()) |shapeCountText| {
- const shapeCount = try shapesCount.addOne(allocator);
- shapeCount.* = try std.fmt.parseInt(u8, shapeCountText, 10);
- }
-
- const area: Area = .{
- .rows = rows,
- .cols = cols,
- .shapesCount = shapesCount.items,
- };
- return area;
- }
- pub fn parseInput(allocator: std.mem.Allocator, inputBuffer: []u8) !Input {
- var shapes = std.ArrayList(Shape).empty;
- var areas = std.ArrayList(Area).empty;
- var i = std.mem.tokenizeSequence(u8, inputBuffer, "\n\n");
- while (i.next()) |inputBlock| {
- if (inputBlock.len >= 2 and inputBlock[1] == ':') {
- const shape = try shapes.addOne(allocator);
- shape.* = parseShape(inputBlock);
- } else {
- var it = std.mem.tokenizeScalar(u8, inputBlock, '\n');
- while(it.next()) |line| {
- const area = try areas.addOne(allocator);
- area.* = try parseArea(allocator, line);
- }
- }
- }
- const input: Input = .{
- .shapes = shapes.items,
- .areas = areas.items,
- };
- return input;
- }
- pub fn readInputIntoString(allocator: std.mem.Allocator, fileName: []const u8) ![]u8 {
- const inputFile = try std.fs.cwd().openFile(
- fileName, .{.mode = .read_only,});
- defer inputFile.close();
- var inputFileReader = inputFile.reader(
- try allocator.alignedAlloc(u8, null, 1024));
- const inputFileSize = try inputFileReader.getSize();
- const inputBuffer = try allocator.alignedAlloc(u8, null, inputFileSize);
- try inputFileReader.interface.readSliceAll(inputBuffer);
- return inputBuffer;
- }
- pub fn main() !void {
- var fb_alloc = std.heap.FixedBufferAllocator.init(&allocatorBuffer);
- const allocator = fb_alloc.allocator();
- const inputBuffer = try readInputIntoString(allocator, "input.txt");
- const input = try parseInput(allocator, inputBuffer);
-
- for (input.areas) |area| {
- try std.fs.File.stdout().writeAll(
- try std.fmt.allocPrint(allocator, "area: rows={d}, cols={d}, shapes=[ ", .{area.rows, area.cols}));
- for (area.shapesCount) |shapeCount| {
- try std.fs.File.stdout().writeAll(
- try std.fmt.allocPrint(allocator, "{d}, ", .{shapeCount}));
- }
- try std.fs.File.stdout().writeAll("]\n");
- }
- for (input.shapes) |shape| {
- try std.fs.File.stdout().writeAll("shape:\n");
- for (0..3) |row| {
- for (0..3) |col| {
- const cell = shape.getCell(@intCast(row), @intCast(col));
- try std.fs.File.stdout().writeAll(
- try std.fmt.allocPrint(allocator, "{c}", .{cell}));
- }
- try std.fs.File.stdout().writeAll("\n");
- }
- try std.fs.File.stdout().writeAll("\n");
- }
- }
|