Created
December 11, 2022 00:38
-
-
Save GolezTrol/e18d4b3eedd7829bdffde2233828823d to your computer and use it in GitHub Desktop.
Advent of Code 2022 Day 9, Rope bridge Planck distance knotting. Somewhat animated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal EnableDelayedExpansion | |
REM MODIFY to tweak speed vs feedback | |
set feedback=5 | |
REM 0 very little, 1 progress on found positions, 3 snapshot animation, 4 step animation, 5 with small delay, 6 with 1 sec delay per step | |
REM #$E# makes prompt echo the escape character, which is then captured and stored in a variable. | |
REM The escape is used for ANSI escape sequences for color an positioning | |
REM ANSI escape codes cheat sheet https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 | |
for /F "delims=#" %%a in ('prompt #$E# ^& for %%a in ^(1^) do rem') do set "esc=%%a" | |
REM input file or defaults | |
set file=%1 | |
if "%file%"=="" set file=Day9.test.txt | |
call :simulate 1 | |
set part1=%result% | |
call :simulate 9 | |
set part2=%result% | |
echo Part1 : %part1% | |
echo Part2 : %part2% | |
exit /b | |
:simulate | |
setlocal | |
REM %1 = tail length | |
REM Starting at 0,0 | |
for /l %%t in (0,1,%1) do ( | |
set X%%t=0 | |
set Y%%t=0 | |
) | |
REM But drawing somewhere mid-screen | |
set offX=40 | |
set offY=11 | |
set count=0 | |
cls | |
for /f "tokens=* delims=" %%a in (%file%) do ( | |
call :process "%%a" %1 | |
) | |
call :count1 %1 | |
echo With tail of length %1: !count!%esc%[K | |
endlocal & set result=%count% | |
exit /b | |
:process | |
REM %1 = COMMAND number | |
REM %2 = tail length | |
REM parse command, %%b is letter, %%c = number | |
for /f "tokens=1,2 delims= " %%b in ("%~1") do ( | |
if %feedback%==3 call :Draw %1 %2 %%b %%c %%c | |
for /l %%d in (1,1,%%c) do ( | |
if %feedback% geq 4 call :Draw %1 %2 %%b %%d %%c | |
call :count1 %2 | |
if %%b==L set /a X0-=1 | |
if %%b==R set /a X0+=1 | |
if %%b==U set /a Y0-=1 | |
if %%b==D set /a Y0+=1 | |
set HX=!X0! | |
set HY=!Y0! | |
for /l %%t in (1,1,%2) do ( | |
set /a Xd=!HX!-!X%%t!&set xd=+!Xd!&set absXd=!Xd:~-1,1! | |
set /a Yd=!HY!-!Y%%t!&set yd=+!Yd!&set absYd=!Yd:~-1,1! | |
set move=true | |
if !absXd! lss 2 if !absYd! lss 2 set move=false | |
if !move!==true ( | |
REM using the sign of Xd/Yd. Note the added + above to always have a sign in the second to last positions | |
if !absXd! neq 0 set /a X%%t=!X%%t!!xd:~-2,1!1 | |
if !absYd! neq 0 set /a Y%%t=!Y%%t!!yd:~-2,1!1 | |
) | |
set HX=!X%%t! | |
set HY=!Y%%t! | |
) | |
if %feedback% geq 5 for /l %%w in (0,1,5000) do rem X | |
if %feedback% geq 6 timeout /t 1 > nul | |
) | |
) | |
exit /b | |
:draw | |
REM %1 Input | |
REM %2 Tail length | |
REM %3 Command | |
REM %4 Step# | |
REM %5 #Steps for command | |
cls | |
echo %esc%[H | |
echo Going %3 %4/%5 | |
echo H ^(!X0!, !Y0!^) T^(!X%2!, !Y%2!^) | |
REM Calculate an offset, to keep the visual in screen. Whenever it wanders off, it's re-centered | |
set /a DrawX=!X0!+!offX! | |
if !DrawX! lss 1 set /a offX=40-!X0! | |
if !DrawX! gtr 80 set /a offX=40-!X0! | |
set /a DrawY=!Y0!+!offY! | |
if !DrawY! lss 1 set /a offY=11-!Y0! | |
if !DrawY! gtr 22 set /a offY=11-!Y0! | |
REM TO DO: iterate over all visible cells and draw something if they have been visited before | |
REM actual drawing of the rope chain | |
for /l %%t in (%2,-1,0) do ( | |
set /a DrawX=!X%%t!+!offX! | |
set /a DrawY=!Y%%t!+!offY! | |
set symbol=%%t | |
if "%%t"=="0" set symbol=H | |
echo %esc%[!DrawY!;!DrawX!f!symbol! | |
) | |
exit /b | |
:count1 | |
REM %1 = tail length / item to count | |
set key=T!X%1!_!Y%1! | |
if "!%key%!"=="" ( | |
set /a count+=1 | |
set %key%=1 | |
if %feedback% geq 1 echo %esc%[HNew cell !key!. Count now at !count! | |
) else if %feedback% geq 2 echo %esc%[H%esc%[K |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
R 4 | |
U 4 | |
L 3 | |
D 1 | |
R 4 | |
D 1 | |
L 5 | |
R 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment