-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.cmd
102 lines (102 loc) · 2.09 KB
/
day14.cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
@echo off
setlocal enabledelayedexpansion
set "output=0"
set "output2=0"
set /a minx=0xffffff
set maxx=0
set /a miny=0xffffff
set maxy=0
for /f "delims=" %%i in ('findstr /N "^" "%~n0.txt"') do (
set line=%%i
set line=!line:*:=!
set line=!line:,=.!
set line=!line: -^> =,!
for %%j in (!line!) do (
for /f "tokens=1,2 delims=." %%x in ("%%j") do (
if defined prev[x] (
for /l %%a in (!prev[x]!,1,%%x) do (
for /l %%b in (!prev[y]!,1,%%y) do (
set area[%%a][%%b]=X
)
for /l %%b in (!prev[y]!,-1,%%y) do (
set area[%%a][%%b]=X
)
)
for /l %%a in (!prev[x]!,-1,%%x) do (
for /l %%b in (!prev[y]!,1,%%y) do (
set area[%%a][%%b]=X
)
for /l %%b in (!prev[y]!,-1,%%y) do (
set area[%%a][%%b]=X
)
)
)
if %%x lss !minx! (
set minx=%%x
) else if %%x gtr !maxx! (
set maxx=%%x
)
if %%y lss !miny! (
set miny=%%y
) else if %%y gtr !maxy! (
set maxy=%%y
)
set prev[x]=%%x
set prev[y]=%%y
)
)
set "prev[x]="
set "prev[y]="
)
set /a floor=%maxy%+2
:sandloop
set x=500
set y=0
set area[%x%][%y%]=O
:fallloop
set /a "tmpy=%y%+1,left=%x%-1,right=%x%+1"
if !tmpy! lss %floor% (
if not defined area[%x%][%tmpy%] (
set "area[%x%][%y%]="
set "area[%x%][%tmpy%]=O"
set /a "x=%x%,y=%tmpy%"
) else (
if not defined area[%left%][%tmpy%] (
set "area[%x%][%y%]="
set "area[%left%][%tmpy%]=O"
set /a "x=%left%,y=%tmpy%"
) else (
if not defined area[%right%][%tmpy%] (
set "area[%x%][%y%]="
set "area[%right%][%tmpy%]=O"
set /a "x=%right%,y=%tmpy%"
) else (
echo found ground: %x%,%y%
if not defined 1done (
set /a output+=1
) else (
set /a output2+=1
if "%x%,%y%"=="500,0" goto endloop
)
goto sandloop
)
)
)
) else (
echo found ground: %x%,%y%
set /a output2+=1
goto sandloop
)
if not defined 1done (
if %tmpy% geq %maxy% (
set 1done=true
set output2=%output%
echo first complete: %output%
)
)
goto fallloop
:endloop
echo %output%
echo %output2%
endlocal
exit /b