Advertisement
SaltyPeaches

Advent of Code: Day 2 (Part 1)

Dec 2nd, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $lines = [IO.File]::ReadAllText("D:\scripts\Sequences.txt") -split "[\r\n]" | where {$_}
  2.  
  3. $grid = New-Object 'object[,]'3,3
  4. $grid[0,0] = 1
  5. $grid[0,1] = 2
  6. $grid[0,2] = 3
  7. $grid[1,0] = 4
  8. $grid[1,1] = 5
  9. $grid[1,2] = 6
  10. $grid[2,0] = 7
  11. $grid[2,1] = 8
  12. $grid[2,2] = 9
  13.  
  14. $x = 1
  15. $y = 1
  16. function CodeDigit($sequencetocheck){
  17.     for($i=0; $i -lt $sequencetocheck.length; $i++){
  18.         if($sequencetocheck[$i] -eq 'U'){
  19.             if ($y -ne 0){
  20.                 $y -= 1
  21.             }
  22.         }
  23.         elseif($sequencetocheck[$i] -eq 'D'){
  24.             if ($y -ne 2){
  25.                 $y += 1
  26.             }
  27.         }
  28.         elseif($sequencetocheck[$i] -eq 'R'){
  29.             if ($x -ne 2){
  30.                 $x += 1
  31.             }
  32.         }
  33.         else{
  34.             if ($x -ne 0){
  35.                 $x -= 1
  36.             }
  37.         }
  38.     }
  39.     return $grid[$y,$x]
  40. }
  41.  
  42. $one = CodeDigit($lines[0])
  43. $two = CodeDigit($lines[1])
  44. $three = CodeDigit($lines[2])
  45. $four = CodeDigit($lines[3])
  46. $five = CodeDigit($lines[4])
  47.  
  48. write-host 'Urination Authorization Code:' $one$two$three$four$five
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement