VB.NET-Quellcode
- Private curGrid As Char()()
- Private chkList As New List(Of Current)
- Sub Main()
- Dim input = IO.File.ReadAllLines(FilePathOfData).Select(Function(l) l.ToCharArray).ToArray
- curGrid = input
- WalkGrid(Direction.East, 0, 0)
- Dim result = chkList.Cast(Of Object).Distinct.Count
- End Sub
- Private Sub WalkGrid(dir As Direction, y As Integer, x As Integer)
- Dim cury = y
- Dim curx = x
- Dim curdir = dir
- Dim cur = New Current With {.X = curx, .Y = cury, .Dir = curdir}
- While Not chkList.Contains(cur)
- chkList.Add(cur)
- curdir = CheckNext(curdir, cury, curx)
- Select Case curdir
- Case Direction.North
- cury -= 1
- Case Direction.West
- curx -= 1
- Case Direction.South
- cury += 1
- Case Else
- curx += 1
- End Select
- If cury < 0 OrElse cury >= curGrid.Length OrElse curx < 0 OrElse curx >= curGrid(0).Length Then Exit While
- cur = New Current With {.X = curx, .Y = cury, .Dir = curdir}
- End While
- End Sub
- Private Function CheckNext(curDir As Direction, cury As Integer, curx As Integer) As Direction
- Dim c = curGrid(cury)(curx)
- If c = "/"c Then
- curDir = CType(If(curDir Mod 2 = 1, curDir + 1, curDir + 3) Mod 4, Direction)
- ElseIf c = "\"c Then
- curDir = CType(If(curDir Mod 2 = 0, curDir + 1, curDir + 3) Mod 4, Direction)
- ElseIf c = "-"c AndAlso curDir Mod 2 = 0 Then
- WalkGrid(CType((curDir + 1) Mod 4, Direction), cury, curx)
- curDir = CType((curDir + 3) Mod 4, Direction)
- ElseIf c = "|"c AndAlso curDir Mod 2 = 1 Then
- WalkGrid(CType((curDir + 3) Mod 4, Direction), cury, curx)
- curDir = CType((curDir + 1) Mod 4, Direction)
- End If
- Return curDir
- End Function
- Friend Enum Direction
- North
- West
- South
- East
- End Enum
- Friend Class Current
- Implements IEquatable(Of Current)
- Property Y As Integer
- Property X As Integer
- Property Dir As Direction
- Public Overloads Function Equals(other As Current) As Boolean Implements IEquatable(Of Current).Equals
- Return _Y = other.Y AndAlso _X = other.X AndAlso _Dir = other.Dir
- End Function
- Public Overrides Function Equals(obj As Object) As Boolean
- Return _Y = DirectCast(obj, Current).Y AndAlso _X = DirectCast(obj, Current).X
- End Function
- Public Overrides Function GetHashCode() As Integer
- Return 1000 * _Y + _X
- End Function
- End Class
Läuft ca. 4 Minuten, habe aber schon eine Idee wie ich den Ansatz aus Part 1 beschleunige, dann sollte Part 2 auch ok sein.
Den größten Teil der hier geprüft wird kann man überspringen.
Den größten Teil der hier geprüft wird kann man überspringen.
VB.NET-Quellcode
- Sub Main()
- Dim input = IO.File.ReadAllLines(FilePathOfData).Select(Function(l) l.ToCharArray).ToArray
- curGrid = input
- WalkGrid(Direction.East, 0, 0)
- Dim resultPart1 = chkList.Cast(Of Object).Distinct.Count
- Dim resutlPart2 = resultPart1
- chkList.Clear()
- For y = 1 To curGrid(0).Length - 1
- WalkGrid(Direction.East, y, 0)
- Dim chk = chkList.Cast(Of Object).Distinct.Count
- If chk > resutlPart2 Then resutlPart2 = chk
- chkList.Clear()
- Next
- For x = 0 To curGrid(0).Length - 1
- WalkGrid(Direction.South, 0, x)
- Dim chk = chkList.Cast(Of Object).Distinct.Count
- If chk > resutlPart2 Then resutlPart2 = chk
- chkList.Clear()
- Next
- For x = 0 To curGrid(0).Length - 1
- WalkGrid(Direction.North, curGrid.Length - 1, x)
- Dim chk = chkList.Cast(Of Object).Distinct.Count
- If chk > resutlPart2 Then resutlPart2 = chk
- chkList.Clear()
- Next
- For y = 0 To curGrid(0).Length - 1
- WalkGrid(Direction.West, y, curGrid(0).Length - 1)
- Dim chk = chkList.Cast(Of Object).Distinct.Count
- If chk > resutlPart2 Then resutlPart2 = chk
- chkList.Clear()
- Next
- End Sub