Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub GetTheCode()
- 'Part1
- Dim iKeypad(2, 2) As Integer
- Dim wks As Worksheet
- Dim sInput As String
- Dim i As Integer
- Dim i2 As Integer
- Dim iX As Integer
- Dim iY As Integer
- iX = 1
- iY = 1
- Set wks = ActiveWorkbook.Sheets("Day2")
- 'build the keypad
- iKeypad(0, 0) = 7
- iKeypad(1, 0) = 8
- iKeypad(2, 0) = 9
- iKeypad(0, 1) = 4
- iKeypad(1, 1) = 5
- iKeypad(2, 1) = 6
- iKeypad(0, 2) = 1
- iKeypad(1, 2) = 2
- iKeypad(2, 2) = 3
- For i = 1 To CountRows("Day2")
- sInput = wks.Cells(i, 1).Value
- For i2 = 1 To Len(sInput)
- Call CheckMove(iX, iY, Mid(sInput, i2, 1))
- Next i2
- Debug.Print iKeypad(iX, iY)
- Next i
- End Sub
- Sub CheckMove(ByRef iX As Integer, ByRef iY As Integer, sMove As String)
- Select Case sMove
- Case "U"
- If iY = 2 Then
- iY = 2
- Else
- iY = iY + 1
- End If
- Case "D"
- If iY = 0 Then
- iY = 0
- Else
- iY = iY - 1
- End If
- Case "L"
- If iX = 0 Then
- iX = 0
- Else
- iX = iX - 1
- End If
- Case "R"
- If iX = 2 Then
- iX = 2
- Else
- iX = iX + 1
- End If
- End Select
- End Sub
- Sub GetTheCodeNow()
- 'Part2 - lets expand the array and use '0's to check for edge
- Dim sKeypad(4, 4) As Variant
- Dim wks As Worksheet
- Dim sInput As String
- Dim i As Integer
- Dim i2 As Integer
- Dim iX As Integer
- Dim iY As Integer
- iX = 0
- iY = 2
- Set wks = ActiveWorkbook.Sheets("Day2")
- 'build the keypad
- sKeypad(0, 0) = 0
- sKeypad(1, 0) = 0
- sKeypad(2, 0) = "D"
- sKeypad(3, 0) = 0
- sKeypad(4, 0) = 0
- sKeypad(0, 1) = 0
- sKeypad(1, 1) = "A"
- sKeypad(2, 1) = "B"
- sKeypad(3, 1) = "C"
- sKeypad(4, 1) = 0
- sKeypad(0, 2) = 5
- sKeypad(1, 2) = 6
- sKeypad(2, 2) = 7
- sKeypad(3, 2) = 8
- sKeypad(4, 2) = 9
- sKeypad(0, 3) = 0
- sKeypad(1, 3) = 2
- sKeypad(2, 3) = 3
- sKeypad(3, 3) = 4
- sKeypad(4, 3) = 0
- sKeypad(0, 4) = 0
- sKeypad(1, 4) = 0
- sKeypad(2, 4) = 1
- sKeypad(3, 4) = 0
- sKeypad(4, 4) = 0
- For i = 1 To CountRows("Day2")
- sInput = wks.Cells(i, 1).Value
- For i2 = 1 To Len(sInput)
- Call CheckMoveNow(iX, iY, sKeypad(), Mid(sInput, i2, 1))
- Next i2
- Debug.Print sKeypad(iX, iY)
- Next i
- End Sub
- Sub CheckMoveNow(ByRef iX As Integer, ByRef iY As Integer, ByRef sKeypad() As Variant, sMove As String)
- Select Case sMove
- Case "U"
- If iY = 4 Then
- iY = 4
- ElseIf Not sKeypad(iX, iY + 1) = 0 Then
- iY = iY + 1
- Else
- iY = iY
- End If
- Case "D"
- If iY = 0 Then
- iY = 0
- ElseIf Not sKeypad(iX, iY - 1) = 0 Then
- iY = iY - 1
- Else
- iY = iY
- End If
- Case "L"
- If iX = 0 Then
- iX = 0
- ElseIf Not sKeypad(iX - 1, iY) = 0 Then
- iX = iX - 1
- Else
- iX = iX
- End If
- Case "R"
- If iX = 4 Then
- iX = 4
- ElseIf Not sKeypad(iX + 1, iY) = 0 Then
- iX = iX + 1
- Else
- iX = iX
- End If
- End Select
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement