Pull to refresh

Мы все ругаем ИИ, и за дело, но вот в ходе дискуссии возник спор: можно ли с помощью ИИ написать код, который будет работать, если человек вообще не владеет нужным языком программирования, но хотя бы чуть-чуть помнит предметную область.

И вот что получилось:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

class LZ77Compressor
{
    const int WINDOW_SIZE = 4096;
    const int LOOKAHEAD_BUFFER_SIZE = 18;

    public static void Compress(string inputPath, string outputPath)
    {
        byte[] input = File.ReadAllBytes(inputPath);
        using var output = new BinaryWriter(File.Create(outputPath));

        int pos = 0;
        while (pos < input.Length)
        {
            int bestMatchDistance = 0, bestMatchLength = 0;

            int startIndex = Math.Max(0, pos - WINDOW_SIZE);

            for (int i = startIndex; i < pos; i++)
            {
                int matchLength = 0;
                while (matchLength < LOOKAHEAD_BUFFER_SIZE &&
                       pos + matchLength < input.Length &&
                       input[i + matchLength] == input[pos + matchLength])
                {
                    matchLength++;
                }

                if (matchLength > bestMatchLength)
                {
                    bestMatchDistance = pos - i;
                    bestMatchLength = matchLength;
                }
            }

            if (bestMatchLength >= 3)
            {
                output.Write((byte)1); // флаг: это ссылка
                output.Write((ushort)bestMatchDistance); // расстояние
                output.Write((byte)bestMatchLength); // длина
                pos += bestMatchLength;
            }
            else
            {
                output.Write((byte)0); // флаг: это литерал
                output.Write(input[pos]); // байт как есть
                pos++;
            }
        }
    }

    public static void Decompress(string inputPath, string outputPath)
    {
        using var input = new BinaryReader(File.OpenRead(inputPath));
        using var output = new BinaryWriter(File.Create(outputPath));

        List<byte> buffer = new();

        while (input.BaseStream.Position < input.BaseStream.Length)
        {
            byte flag = input.ReadByte();
            if (flag == 1)
            {
                int distance = input.ReadUInt16();
                int length = input.ReadByte();

                int start = buffer.Count - distance;
                for (int i = 0; i < length; i++)
                {
                    byte b = buffer[start + i];
                    buffer.Add(b);
                    output.Write(b);
                }
            }
            else
            {
                byte b = input.ReadByte();
                buffer.Add(b);
                output.Write(b);
            }
        }
    }
}
class Program
{
    static void Main()
    {
        string input = "input.txt";
        string compressed = "compressed.lz";
        string decompressed = "output.txt";

        LZ77Compressor.Compress(input, compressed);
        LZ77Compressor.Decompress(compressed, decompressed);

        Console.WriteLine("Сжатие и распаковка завершены.");
    }
}

Я не знаю С#, и даже не помню LZ - кто-нибудь может попытаться это скомпилировать - оно работает или нет?
Или опять ИИ всё навыдумывал?

Tags:
-8
Comments8

Articles