-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask112.au3
82 lines (76 loc) · 2.91 KB
/
Task112.au3
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
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
Func Task112()
Local $hFileOpen = FileOpen("map.txt", $FO_READ)
If $hFileOpen = -1 Then
ConsoleWrite("An error occurred when reading the file.")
Return False
EndIf
Local $sFileRead = FileRead($hFileOpen)
FileClose($hFileOpen)
Dim $lines = StringSplit($sFileRead, @CRLF, $STR_ENTIRESPLIT)
Dim $colCounts[StringLen($lines[1])]
Dim $rowCounts[$lines[0]]
Dim $gal[$lines[0]][UBound($colCounts)]
$galCount = 0
; Expand array
For $i = 1 To $lines[0]
For $j = 1 to StringLen($lines[$i])
$ss = StringMid($lines[$i], $j, 1)
If ($ss <> ".") Then
$colCounts[$j-1] = $colCounts[$j-1] + 1
$rowCounts[$i-1] = $rowCounts[$i-1] + 1
EndIf
$gal[$i-1][$j-1] = $ss
If $ss = "#" Then
$galCount = $galCount + 1
EndIf
Next
Next
$totPaths = 0
For $i = 1 To $galCount-1
$totPaths = $totPaths + $i
Next
ConsoleWrite("Galaxies: " & $galCount & @CRLF)
ConsoleWrite("Paths: " & $totPaths & @CRLF)
Dim $paths[$totPaths]
$jump = 1000000
$totSum = 0
$currentPath = 0
$yJump1 = 0
For $y1 = 0 To $lines[0]-1
If $rowCounts[$y1] = '' Then
$yJump1 = $yJump1 + ($jump-1)
EndIf
$xJump1 = 0
For $x1 = 0 To UBound($colCounts)-1
If $colCounts[$x1] = '' Then
$xJump1 = $xJump1 + ($jump-1)
EndIf
If $gal[$y1][$x1] = "#" Then
$yJump2 = $yJump1
For $y2 = $y1 To $lines[0]-1
If $y2 > $y1 and $rowCounts[$y2] = '' Then
$yJump2 = $yJump2 + ($jump-1)
EndIf
$xJump2 = 0
For $x2 = 0 To UBound($colCounts)-1
If $colCounts[$x2] = '' Then
$xJump2 = $xJump2 + ($jump-1)
EndIf
If $gal[$y2][$x2] = "#" and (($y2 > $y1) or ($x2 > $x1)) Then
$pSize = Abs(($y2 + $yJump2) - ($y1 + $yJump1)) + Abs(($x2 + $xJump2) - ($x1 + $xJump1))
;ConsoleWrite(" Size: " & $pSize & @CRLF)
$paths[$currentPath] = $pSize
$totSum = $totSum + $pSize
$currentPath = $currentPath + 1
EndIf
Next
Next
EndIf
Next
Next
ConsoleWrite("Sum: " & $totSum & @CRLF)
EndFunc
Task112()