You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本优化挑战:将代码压缩至259字符以内

PowerShell代码高尔夫优化方案

核心优化措施

  • 用PowerShell内置缩写压缩命令:gc替代Get-Content,-As替代-AssemblyName,-ms替代-Milliseconds,%替代ForEach-Object
  • 简化类型转换逻辑:直接通过([int]$_).ToString(2)生成二进制字符串,省去[Convert]类调用
  • 用三元运算符替换分支判断:($b-eq'1')?'+{CAPSLOCK}':'{NUMLOCK}'压缩if-else代码块
  • 移除冗余变量:直接引用$env:TEMP\z路径,无需额外存储文件对象
  • 合并语句:将加载程序集、读取文件、循环处理逻辑用分号串联,减少冗余字符

压缩后代码(字符数约220,满足259以内要求)

Add-Type -As Windows.Forms;$c=gc "$env:TEMP\z" -Raw;$c|%{foreach($b in ([int]$_).ToString(2).PadLeft(8,'0').ToCharArray()){[Windows.Forms.SendKeys]::SendWait(($b-eq'1')?'+{CAPSLOCK}':'{NUMLOCK}');Start-Sleep -ms 100}}

运行方式

直接在系统运行框(Win+R)中执行:

powershell -Command "Add-Type -As Windows.Forms;$c=gc "$env:TEMP\z" -Raw;$c|%{foreach($b in ([int]$_).ToString(2).PadLeft(8,'0').ToCharArray()){[Windows.Forms.SendKeys]::SendWait(($b-eq'1')?'+{CAPSLOCK}':'{NUMLOCK}');Start-Sleep -ms 100}}"

内容的提问来源于stack exchange,提问作者I am Jakoby

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 20:48:46