-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.vb
138 lines (114 loc) · 5.24 KB
/
Day10.vb
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
Imports System.IO
Module Day10
Public Sub DoDay10()
Dim Fs As New FileStream("E:\AOC\inputs\input-day10.txt", FileMode.Open, FileAccess.Read) ' AOC1 input
Dim SR As New StreamReader(Fs)
SR.BaseStream.Seek(0, SeekOrigin.Begin)
Dim Data As String = "[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]"
Dim LenSample As Int16 = 100
Dim board = New String(LenSample) {} ', LenSample) {}
Dim board_Bool = New Boolean(LenSample, LenSample) {}
Dim Line As String = ""
Dim CntColumn As Int16 = 1
Dim CntRows As Int16 = 1
Dim Result = New Integer(LenSample) {}
Dim Indx_Result As Integer = 1
Dim Corrupted As Boolean = False
Dim Cursor As Int16 = 1
Dim SumResult As Integer = 0
Dim CompleteResult = New Long(LenSample) {}
Dim BoardCompleteResult As String = ""
Dim Indx_CompleteResult As Integer = 1
While SR.Peek > -1
Line = SR.ReadLine()
' For Each Line In Data.Split(ControlChars.CrLf.ToCharArray)
If Line = "" Then Continue While ' For '
Corrupted = False
Cursor = 1
For CntColumn = 1 To Line.Length
Select Case Mid(Line, CntColumn, 1)
Case "(", "[", "{", "<"
board(Cursor) = Mid(Line, CntColumn, 1)
Cursor += 1
Case Else
If Mid(Line, CntColumn, 1) = ")" And board(Cursor - 1) = "(" Or
Mid(Line, CntColumn, 1) = "]" And board(Cursor - 1) = "[" Or
Mid(Line, CntColumn, 1) = "}" And board(Cursor - 1) = "{" Or
Mid(Line, CntColumn, 1) = ">" And board(Cursor - 1) = "<" Then
Cursor -= 1
Else
Corrupted = True
Exit For
End If
End Select
Next
If Corrupted Then
Select Case Mid(Line, CntColumn, 1)
Case ")"
SumResult += 3
Case "]"
SumResult += 57
Case "}"
SumResult += 1197
Case ">"
SumResult += 25137
End Select
Indx_Result += 1
Else
BoardCompleteResult = ""
For k As Int16 = Cursor - 1 To 1 Step -1
Select Case board(k)
Case "("
BoardCompleteResult &= ")"
Case "["
BoardCompleteResult &= "]"
Case "{"
BoardCompleteResult &= "}"
Case "<"
BoardCompleteResult &= ">"
End Select
Next
For k As Int16 = 1 To BoardCompleteResult.Length
CompleteResult(Indx_CompleteResult) = CompleteResult(Indx_CompleteResult) * 5 +
IIf(Mid(BoardCompleteResult, k, 1) = ")", 1,
IIf(Mid(BoardCompleteResult, k, 1) = "]", 2,
IIf(Mid(BoardCompleteResult, k, 1) = "}", 3, 4)))
Next
Indx_CompleteResult += 1
End If
CntRows += 1
' Next
End While
Array.Sort(CompleteResult)
Array.Reverse(CompleteResult)
MsgBox(CompleteResult(CInt((Indx_CompleteResult - 1) / 2)))
SR.Close()
End Sub
Private Function FindRecursiveNums(board_Bool As Boolean(,), board As Integer(,),
i As Int16, j As Int16, CntColumn As Int16, CntRows As Int16,
Optional Left As Boolean = True, Optional Rigth As Boolean = True,
Optional Top As Boolean = True, Optional Bottom As Boolean = True) As Int16
board_Bool(i, j) = True
If board(i, j) = 9 Then
Return 0
Else
Dim Cnt As Int16 = 0
If j - 1 > 0 AndAlso board_Bool(i, j - 1) = False Then Cnt += FindRecursiveNums(board_Bool, board, i, j - 1, CntColumn, CntRows)
If j + 1 <= CntColumn AndAlso board_Bool(i, j + 1) = False Then Cnt += FindRecursiveNums(board_Bool, board, i, j + 1, CntColumn, CntRows)
If i - 1 > 0 AndAlso board_Bool(i - 1, j) = False Then Cnt += FindRecursiveNums(board_Bool, board, i - 1, j, CntColumn, CntRows)
If i + 1 <= CntRows AndAlso board_Bool(i + 1, j) = False Then Cnt += FindRecursiveNums(board_Bool, board, i + 1, j, CntColumn, CntRows)
'If Cnt = 0 Then
Cnt += 1
Return Cnt
End If
End Function
End Module