OpenAI 在精简代码方面的出色表现令人震惊

科技堆里的狠货 2025-05-02 阅读:5176 评论:0

本文转载自https://glama.ai/blog/2024-08-29-reverse-engineering-minified-code-using-openai

OpenAI 在精简代码方面的出色表现令人震惊   在浏览互联网寻找灵感时,我发现了一个看起来很有趣的组件:

https://reactive.network/hackathon

我觉得带有运行 ASCII 艺术的区块看起来很酷,但我不太明白它是如何实现的,于是我开始查看源代码。

我找到了看起来可以完成这项工作的代码,但它被精简化了。

const { floor: ra, abs: KE, min: QE } = Math,    O5 = ["reactive.network REACTIVE.NETWORK", "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,^`'. .:â–‘â–’â–“â–ˆ"],    G7 = Date.now() % 3 ? O5[1] : O5[0],    V5 = G7.length,    JE = { fps: 60 };function eT(e, t, n, r) {    const i = t.time * 8e-5,        s = QE(t.cols, t.rows),        o = t.metrics.aspect * 0.2,        l = { x: ((4 * (e.x - t.cols / 6.25)) / s) * o, y: (5 * (e.y - t.rows / 4)) / s },        u = ra(KE(YE(l) - i) * V5 + (ra(e.x / 1) % 2) * 2) % V5;    return G7[u];}const tT = () => {    const e = j.useRef(null),        [t, n] = j.useState({ height: null, width: null });    return (        j.useEffect(() => {            function r() {                n({ height: window.innerHeight, width: window.innerWidth });            }            if (typeof window < "u") return n({ height: window.innerHeight, width: window.innerWidth }), window.addEventListener("resize", r), () => window.removeEventListener("resize", r);        }, []),        j.useEffect(() => {            const r = e.current;            if (!r) return;            const i = 12,                s = ra(t.width / i) * 1.6,                o = ra(t.height / i),                l = { aspect: s / o },                u = setInterval(() => {                    let c = "";                    for (let d = 0; d < o; d++) {                        for (let f = 0; f < s; f++) c += eT({ x: f, y: d }, { cols: s, rows: o, metrics: l, time: Date.now() });                        c += ``;                    }                    r.textContent = c;                }, 1e3 / JE.fps);            return () => clearInterval(u);        }, [t]),        a.jsx("p", { style: { position: "absolute", top: 0, left: 0, width: "100%", height: "100%" }, children: a.jsx("p", { ref: e, style: { width: "100%", height: "100%", whiteSpace: "pre", overflow: "hidden" } }) })    );};function nT(e) {    return Math.cos(e.x * e.x - e.y * e.y);}const { floor: ia, abs: rT, min: iT } = Math,    D5 = ["reactive.network REACTIVE.NETWORK", "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,^`'. .:â–‘â–’â–“â–ˆ"],    X7 = Date.now() % 3 ? D5[1] : D5[0],    F5 = X7.length,    sT = { fps: 60 };function oT(e, t, n, r) {    const i = t.time * 8e-5,        s = iT(t.cols, t.rows),        o = t.metrics.aspect * 0.2,        l = { x: ((4 * (e.x - t.cols / 6.25)) / s) * o, y: (5 * (e.y - t.rows / 4)) / s },        u = ia(rT(nT(l) - i) * F5 + (ia(e.x / 1) % 2) * 2) % F5;    return X7[u];}const aT = () => {    const e = j.useRef(null),        [t, n] = j.useState({ height: null, width: null });    return (        j.useEffect(() => {            function r() {                n({ height: window.innerHeight, width: window.innerWidth });            }            if (typeof window < "u") return n({ height: window.innerHeight, width: window.innerWidth }), window.addEventListener("resize", r), () => window.removeEventListener("resize", r);        }, []),        j.useEffect(() => {            const r = e.current;            if (!r) return;            const i = 12,                s = ia(t.width / i) * 1.6,                o = ia(t.height / i),                l = { aspect: s / o },                u = setInterval(() => {                    let c = "";                    for (let d = 0; d < o; d++) {                        for (let f = 0; f < s; f++) c += oT({ x: f, y: d }, { cols: s, rows: o, metrics: l, time: Date.now() });                        c += ``;                    }                    r.textContent = c;                }, 1e3 / sT.fps);            return () => clearInterval(u);        }, [t]),        a.jsx("p", { style: { position: "absolute", top: 0, left: 0, width: "100%", height: "100%" }, children: a.jsx("p", { ref: e, style: { width: "100%", height: "100%", whiteSpace: "pre", overflow: "hidden" } }) })    );},
通常情况下,我会通过阅读最小化的代码来理解其实现(或者查找 .map 文件来还原原始代码)。不过,我意识到我从未尝试过让 ChatGPT 帮我做这件事...... 于是,我复制了上述所有代码,并要求 ChatGPT “解释代码”。以下是回复:

提供的代码非常复杂,但我会将其分解为更易于理解的格式,解释其不同部分及其功能。
概述该代码使用 JavaScript 定义了几个函数和组件,特别是在 React 应用程序的上下文中。它处理使用预定义字符集中的字符生成一些动态文本内容,可能是 ASCII 艺术或某种视觉效果。此外,它还包括用于事件处理、性能计时和 HTML 内容转换的实用函数。
关键部件和功能字符集选择:
const { floor: ra, abs: KE, min: QE } = Math;const O5 = ["reactive.network REACTIVE.NETWORK", "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,^`'. .:â–‘â–’â–“â–ˆ"];const G7 = Date.now() % 3 ? O5[1] : O5[0];const V5 = G7.length;这部分代码导入了一些常见的数学函数(floor、、abs和min),并定义了两组字符,存储在中O5。
根据当前时间(Date.now() % 3),它会选择其中一个集合(G7)以供稍后使用。
所选字符集的长度存储在中V5。
动态角色选择功能:
function eT(e, t, n, r) {    const i = t.time * 8e-5;    const s = QE(t.cols, t.rows);    const o = t.metrics.aspect * 0.2;    const l = { x: ((4 * (e.x - t.cols / 6.25)) / s) * o, y: (5 * (e.y - t.rows / 4)) / s };    const u = ra(KE(YE(l) - i) * V5 + (ra(e.x / 1) % 2) * 2) % V5;    return G7[u];}该eT函数根据输入位置(e.x,e.y)和当前时间(t.time)生成一个字符。
它使用一些纵横比计算来调整位置,并根据G7位置和时间的组合选择一个字符。
YE从代码中无法直接看出该函数的用途,但它似乎是此计算中使用的自定义函数。
tT用于显示动态内容的React 组件( ):
const tT = () => {    const e = j.useRef(null);    const [t, n] = j.useState({ height: null, width: null });    j.useEffect(() => {        function r() {            n({ height: window.innerHeight, width: window.innerWidth });        }        if (typeof window < "u") {            n({ height: window.innerHeight, width: window.innerWidth });            window.addEventListener("resize", r);            return () => window.removeEventListener("resize", r);        }    }, []);
    j.useEffect(() => {        const r = e.current;        if (!r) return;        const i = 12;        const s = ra(t.width / i) * 1.6;        const o = ra(t.height / i);        const l = { aspect: s / o };        const u = setInterval(() => {            let c = "";            for (let d = 0; d < o; d++) {                for (let f = 0; f < s; f++) c += eT({ x: f, y: d }, { cols: s, rows: o, metrics: l, time: Date.now() });                c += ``;            }            r.textContent = c;        }, 1e3 / JE.fps);        return () => clearInterval(u);    }, [t]);
    return a.jsx("p", { style: { position: "absolute", top: 0, left: 0, width: "100%", height: "100%" }, children: a.jsx("p", { ref: e, style: { width: "100%", height: "100%", whiteSpace: "pre", overflow: "hidden" } }) });};这是一个 React 组件,它设置一个间隔,以使用动态生成的字符定期更新p元素的内容( )。e.current
它根据窗口大小计算列数(s)和行数( ),然后用函数生成的字符填充。opeT
该效果以每秒指定的帧数运行(JE.fps)。
我继续询问“用 TypeScript 实现等效代码并使其易于阅读”,并得到了以下答复:

import React, { useEffect, useRef, useState } from 'react';
const selectedCharacterSet =  "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,^`'. .:â–‘â–’â–“â–ˆ";const characterSetLength = selectedCharacterSet.length;
const calculateCharacter = (  x: number,  y: number,  cols: number,  rows: number,  aspect: number,  time: number,): string => {  const timeFactor = time * 0.000_08;  const size = Math.min(cols, rows);  const aspectRatio = aspect * 0.2;  const position = {    x: ((4 * (x - cols / 6.25)) / size) * aspectRatio,    y: (5 * (y - rows / 4)) / size,  };
  const index =    Math.floor(      Math.abs(        Math.cos(position.x * position.x - position.y * position.y) -          timeFactor,      ) *        characterSetLength +        (Math.floor(x) % 2) * 2,    ) % characterSetLength;  return selectedCharacterSet[index];};
type Size = {  height: null | number;  width: null | number;};
const AsciiArtGenerator: React.FC = () => {  const textRef = useRef
 
  (null);  const [size, setSize] = useState
  
   ({ height: null, width: null }); useEffect(() => { const handleResize = () => { setSize({ height: window.innerHeight, width: window.innerWidth }); }; // Initial size setting handleResize(); window.
   addEventListener('resize', handleResize); return () => { window.removeEventListener('resize', handleResize); }; }, []); useEffect(() => { const element = textRef.current; if (!element || !size.width || !size.height) return undefined; const cols = Math.floor(size.width / 12) * 1.6; const rows = Math.floor(size.height / 12); const aspectRatio = cols / rows; const intervalId = setInterval(() => { let content = ''; for (let y = 0; y < rows; y++) { for (let x = 0; x < cols; x++) { content += calculateCharacter( x, y, cols, rows, aspectRatio, Date.now(), ); } content += 'n'; } element.textContent = content; }, 1_000 / 60); // 60 fps return () => { clearInterval(intervalId); }; }, [size]); return (  );}; export default () => { return 
   ;};
  
 
令我惊讶的是,响应不仅足够好,而且可读性也很强。

上述组件的实际运行情况如下:

比较输出结果,LLM 响应似乎忽略了一些实现细节,但它仍然是一个值得学习的好实现。这绝对是一个我以前从未考虑过的巧妙用例!

注意:如果想第一时间收到 KiCad 内容推送,请点击下方的名片,按关注,再设为星标。

常用合集汇总:

和 Dr Peter 一起学 KiCad

KiCad 8 探秘合集

KiCad 使用经验分享

KiCad 设计项目(Made with KiCad)

常见问题与解决方法

KiCad 开发笔记

审核编辑 黄宇

热门文章
  • 防风防寒!北京今天晴朗伴大风寒意十足 周末将迎小幅升温

    防风防寒!北京今天晴朗伴大风寒意十足 周末将迎小幅升温
      中国天气网讯 今天(12月27日),北京天气晴间多云,最高气温2℃,白天北风劲吹,阵风可达六至七级,风寒效应明显。本周末,北京仍以晴为主,风力不大,气温将有小幅上升。   昨天,北京晴冷在线,气温继续下跌,南郊观象台最高气温仅有2.6℃,加上风力较大,体感十分寒冷。   北京市气象台预计,今天白天晴间多云,北风三四级(阵风六七级),最高气温2℃;夜间晴间多云,北风二三级间四级,最低气温零下7℃。   明后两天,北京仍以晴为主,风力不大,最高气温将略升至5℃,最低气温...
  • 在中超联赛赛场北京成都球迷高呼:北京加油,成都雄起

    在中超联赛赛场北京成都球迷高呼:北京加油,成都雄起
      9月14日晚,中超联赛成都蓉城对北京国安的比赛在成都凤凰山体育场举行。首都文明办工作人员到现场力促两地球迷的友好互动,引导球迷文明观赛。   开赛前,两地球迷早早来到赛场,成都球迷在赛场通道为等待入场的北京球迷拉歌拍照。一边是北京球迷激昂的歌声,一边是身穿“雄起”“成都”等字样球衣的成都球迷,画面和谐温馨。首都文明办给两地球迷代表赠送了“向北京榜样学习”宣传品,呼吁两地球迷在场上是对手,在场下是朋友。合影留念时,成都球迷高喊“北京加油”,北京球迷高喊“成都雄起”。...
  • 新手如何开始跑步?

    新手如何开始跑步?
    大家好,我是小贝~ 有喜欢我的分享的可以给我点个关哟~多多互动吧~🫰 跑步是最简单的运动之一。人类进化30万年,跑步是基因自带的能力。可以说天生人人都会跑,人人都可以跑。 所以,很多平时不跑步的人,想入门跑步,建议从以下4个方面开始 一、跑步一定要穿跑鞋! 10年前我跑步穿平时的休闲鞋跑了2周,跟腱受伤!因为休闲鞋没有缓震效果;对膝盖和跟腱的损害较大。 专业跑鞋鞋底有缓震设计,能减少跑步时对膝盖和脚踝的冲击力。保护膝盖和脚踝不容易受伤。 二、注意跑步频率和强度。 1、频率...
  • 西南地区持续阴雨天气 华北黄淮等地大气扩散条件逐步转差

    西南地区持续阴雨天气 华北黄淮等地大气扩散条件逐步转差
      摘要:   国内方面,昨日,全国降水整体较弱;内蒙古、东北地区等地出现大风降温天气。未来三天,青藏高原及云南、四川、贵州等地多阴雨天气,关注局地强降雨或持续降雨可能引发的次生灾害。   全球方面,昨日,欧洲东部美国东南部等地出现强降雨。未来三天,飓风“米尔顿”继续影响美国东南部等地;强冷空气影响中亚等地;欧洲大部大范围降水降温。   一、国内天气情况   1.实况   全国降水整体较弱 内蒙古东北地区等地出现大风降温天气   昨日8时至今日6时,全国降水整体较...
  • 大雾黄色预警:京津冀等8省市部分地区有大雾 局地强浓雾

    大雾黄色预警:京津冀等8省市部分地区有大雾 局地强浓雾
      据报道10月14日电据中央气象台网站消息,预计10月14日早晨至上午,河北中南部、北京、天津西部、山东西部、山西中东部、陕西北部、河南东北部和南部部分地区、湖北中部等地有大雾天气,其中,河北中南部、北京西部、山西中部、陕西北部、湖北中部等地的部分地区有能见度低于500米的浓雾,局地有不足200米的强浓雾。中央气象台14日6时继续发布大雾黄色预警。   此外,14日,华北中南部、黄淮中西部、汾渭平原等地大气扩散条件较差,有轻至中度霾,其中,北京南部、河北西部沿山部分地区有...
标签列表