Advertisement
Bogzla

AoC_2016_Day2

Dec 2nd, 2016
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.61 KB | None | 0 0
  1.     Sub GetTheCode()
  2.     'Part1
  3.     Dim iKeypad(2, 2) As Integer
  4.     Dim wks As Worksheet
  5.     Dim sInput As String
  6.     Dim i As Integer
  7.     Dim i2 As Integer
  8.     Dim iX As Integer
  9.     Dim iY As Integer
  10.     iX = 1
  11.     iY = 1
  12.     Set wks = ActiveWorkbook.Sheets("Day2")
  13.    
  14.     'build the keypad
  15.     iKeypad(0, 0) = 7
  16.     iKeypad(1, 0) = 8
  17.     iKeypad(2, 0) = 9
  18.     iKeypad(0, 1) = 4
  19.     iKeypad(1, 1) = 5
  20.     iKeypad(2, 1) = 6
  21.     iKeypad(0, 2) = 1
  22.     iKeypad(1, 2) = 2
  23.     iKeypad(2, 2) = 3
  24.    
  25.     For i = 1 To CountRows("Day2")
  26.         sInput = wks.Cells(i, 1).Value
  27.         For i2 = 1 To Len(sInput)
  28.             Call CheckMove(iX, iY, Mid(sInput, i2, 1))
  29.         Next i2
  30.         Debug.Print iKeypad(iX, iY)
  31.     Next i
  32.     End Sub
  33.    
  34.     Sub CheckMove(ByRef iX As Integer, ByRef iY As Integer, sMove As String)
  35.     Select Case sMove
  36.         Case "U"
  37.             If iY = 2 Then
  38.                 iY = 2
  39.             Else
  40.                 iY = iY + 1
  41.             End If
  42.         Case "D"
  43.             If iY = 0 Then
  44.                 iY = 0
  45.             Else
  46.                 iY = iY - 1
  47.             End If
  48.         Case "L"
  49.             If iX = 0 Then
  50.                 iX = 0
  51.             Else
  52.                 iX = iX - 1
  53.             End If
  54.         Case "R"
  55.             If iX = 2 Then
  56.                 iX = 2
  57.             Else
  58.                 iX = iX + 1
  59.             End If
  60.     End Select
  61.     End Sub
  62.    
  63.     Sub GetTheCodeNow()
  64.     'Part2 - lets expand the array and use '0's to check for edge
  65.     Dim sKeypad(4, 4) As Variant
  66.     Dim wks As Worksheet
  67.     Dim sInput As String
  68.     Dim i As Integer
  69.     Dim i2 As Integer
  70.     Dim iX As Integer
  71.     Dim iY As Integer
  72.     iX = 0
  73.     iY = 2
  74.     Set wks = ActiveWorkbook.Sheets("Day2")
  75.    
  76.    
  77.     'build the keypad
  78.     sKeypad(0, 0) = 0
  79.     sKeypad(1, 0) = 0
  80.     sKeypad(2, 0) = "D"
  81.     sKeypad(3, 0) = 0
  82.     sKeypad(4, 0) = 0
  83.     sKeypad(0, 1) = 0
  84.     sKeypad(1, 1) = "A"
  85.     sKeypad(2, 1) = "B"
  86.     sKeypad(3, 1) = "C"
  87.     sKeypad(4, 1) = 0
  88.     sKeypad(0, 2) = 5
  89.     sKeypad(1, 2) = 6
  90.     sKeypad(2, 2) = 7
  91.     sKeypad(3, 2) = 8
  92.     sKeypad(4, 2) = 9
  93.     sKeypad(0, 3) = 0
  94.     sKeypad(1, 3) = 2
  95.     sKeypad(2, 3) = 3
  96.     sKeypad(3, 3) = 4
  97.     sKeypad(4, 3) = 0
  98.     sKeypad(0, 4) = 0
  99.     sKeypad(1, 4) = 0
  100.     sKeypad(2, 4) = 1
  101.     sKeypad(3, 4) = 0
  102.     sKeypad(4, 4) = 0
  103.    
  104.     For i = 1 To CountRows("Day2")
  105.         sInput = wks.Cells(i, 1).Value
  106.         For i2 = 1 To Len(sInput)
  107.             Call CheckMoveNow(iX, iY, sKeypad(), Mid(sInput, i2, 1))
  108.         Next i2
  109.         Debug.Print sKeypad(iX, iY)
  110.     Next i
  111.     End Sub
  112.     Sub CheckMoveNow(ByRef iX As Integer, ByRef iY As Integer, ByRef sKeypad() As Variant, sMove As String)
  113.     Select Case sMove
  114.         Case "U"
  115.             If iY = 4 Then
  116.                 iY = 4
  117.             ElseIf Not sKeypad(iX, iY + 1) = 0 Then
  118.                 iY = iY + 1
  119.             Else
  120.                 iY = iY
  121.             End If
  122.         Case "D"
  123.             If iY = 0 Then
  124.                 iY = 0
  125.             ElseIf Not sKeypad(iX, iY - 1) = 0 Then
  126.                 iY = iY - 1
  127.             Else
  128.                 iY = iY
  129.             End If
  130.         Case "L"
  131.             If iX = 0 Then
  132.                 iX = 0
  133.             ElseIf Not sKeypad(iX - 1, iY) = 0 Then
  134.                 iX = iX - 1
  135.             Else
  136.                 iX = iX
  137.             End If
  138.         Case "R"
  139.             If iX = 4 Then
  140.                 iX = 4
  141.             ElseIf Not sKeypad(iX + 1, iY) = 0 Then
  142.                 iX = iX + 1
  143.             Else
  144.                 iX = iX
  145.             End If
  146.     End Select
  147.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement