Heute war's eigentlich recht leicht. Ich musste mich aber überwinden anzufangen — Meh, Rekursion, meh ”1000” Sonderfälle beachten. Aber wie gesagt, war dann doch einfach.
framp Die übrig gebliebene Zeit habe ich dann genutzt Tag 11 noch mal in FreeBASIC mit OOP zu schreiben:
Display Spoiler
Visual Basic
Const IMAGE_FILENAME = "input11.txt"
'Const IMAGE_FILENAME = "exampl11.txt"
Type TGalaxy
x As Integer
y As Integer
Declare Constructor ()
Declare Constructor (x As Integer, y As Integer)
Declare Const Function DistanceTo (other As Const TGalaxy) As Integer
End Type
Constructor TGalaxy ()
End Constructor
Constructor TGalaxy (x As Integer, y As Integer)
This.x = x: This.y = y
End Constructor
Const Function TGalaxy.DistanceTo (other As Const TGalaxy) As Integer
Return Abs(x - other.x) + Abs(y - other.y)
End Function
Type TUniverse
Private:
galaxies(Any) As TGalaxy
rowHasGalaxy(Any) As Boolean
columnHasGalaxy(Any) As Boolean
Public:
Declare Constructor (filename As String)
Declare Const Property compressedSize () As Integer
Declare Const Sub ExpandInto (factor As Integer, galaxies() As TGalaxy)
Declare Sub Load (filename As String)
End Type
Constructor TUniverse (filename As String)
Load filename
End Constructor
Const Property TUniverse.compressedSize () As Integer
Assert(UBound(rowHasGalaxy) = UBound(columnHasGalaxy))
Return UBound(rowHasGalaxy)
End Property
Const Sub TUniverse.ExpandInto (factor As Integer, galaxies() As TGalaxy)
Dim i As Integer
Dim x As Integer, y As Integer
Dim expandedX(compressedSize) As Integer
Dim expandedY(compressedSize) As Integer
x = 0: y = 0
For i = 0 To compressedSize
x = x + IIf(columnHasGalaxy(i), 1, factor)
expandedX(i) = x
y = y + IIf(rowHasGalaxy(i), 1, factor)
expandedY(i) = y
Next
ReDim galaxies(UBound(This.galaxies))
For i = 1 To UBound(galaxies)
With This.galaxies(i)
galaxies(i) = TGalaxy(expandedX(.x), expandedY(.y))
End With
Next
End Sub
Sub TUniverse.Load (filename As String)
Dim f As Integer, n As Integer, i As Integer, j As Integer
Dim row As String
' Load the first row to get the size of the image.
f = FreeFile
Open filename For Input As f
Line Input #f, row
Close f
ReDim rowHasGalaxy(Len(row))
ReDim columnHasGalaxy(Len(row))
' Start with enough space that each coordinate may contain a galaxy.
ReDim galaxies(1 To compressedSize^2)
n = 0: i = 0
Open filename For Input As f
Do While Not Eof(f)
Line Input #f, row
Assert(Len(row) = compressedSize)
For j = 0 To Len(row) - 1
If Mid$(row, j + 1, 1) = "#" Then
n = n + 1
galaxies(n) = TGalaxy(j, i)
rowHasGalaxy(i) = TRUE
columnHasGalaxy(j) = TRUE
End If
Next
i = i + 1
Loop
Close f
Assert(i = compressedSize)
ReDim Preserve galaxies(1 To n)
End Sub
Function SumPathLengths (universe As Const TUniverse, factor As Integer) As Integer
Dim galaxies(Any) As TGalaxy
Dim i As Integer, j As Integer, total As Integer
universe.ExpandInto factor, galaxies()
total = 0
For i = 1 to UBound(galaxies) - 1
For j = i + 1 to UBound(galaxies)
total = total + galaxies(i).DistanceTo(galaxies(j))
Next
Next
Return total
End Function
Dim universe As TUniverse = (IMAGE_FILENAME)
Print SumPathLengths(universe, 2)
Print SumPathLengths(universe, 1000000)
Display More