Advertisement
Guest User

Untitled

a guest
Dec 12th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ---------------------------- Advent of code 2020 Day 12 --------------------------- #
  2.  
  3. # Get input
  4. Clear-Host
  5. [System.Collections.Generic.List[psobject]]$puzzleInput = Get-Content -Path "$env:USERPROFILE\Downloads\input.txt"
  6.  
  7. $instructions = $puzzleInput | ForEach-Object {
  8.  
  9.     [PSCustomObject]@{
  10.         "Direction" = $_.Substring(0, 1)
  11.         "Moves"     = $_.Substring(1)
  12.        
  13.     }
  14.  
  15. }
  16.  
  17. $currentDirection = 90
  18.  
  19. $currentPosition = @{"NorthSouth" = 0; "EastWest" = 0 }
  20.  
  21. foreach ($instruction in $instructions) {
  22.  
  23.     "Currently at $($currentPosition.Values)"
  24.     "Current direction is $currentDirection"
  25.    
  26.     switch ($instruction.direction) {
  27.  
  28.         "N" {
  29.            
  30.             "Now moving North by $($instruction.moves)"
  31.  
  32.             $currentPosition.NorthSouth = $currentPosition.NorthSouth + $instruction.moves
  33.         }
  34.  
  35.         "S" {
  36.            
  37.             "Now moving South by $($instruction.moves)"
  38.  
  39.             $currentPosition.NorthSouth = $currentPosition.NorthSouth - $instruction.moves
  40.    
  41.         }
  42.  
  43.         "E" {
  44.            
  45.             "Now moving East by $($instruction.moves)"
  46.  
  47.             $currentPosition.EastWest = $currentPosition.EastWest + $instruction.moves
  48.    
  49.         }
  50.  
  51.         "W" {
  52.            
  53.             "Now moving West by $($instruction.moves)"
  54.  
  55.             $currentPosition.EastWest = $currentPosition.EastWest - $instruction.moves
  56.        
  57.         }
  58.  
  59.         "L" {
  60.            
  61.             "Now turning Left by $($instruction.moves)"
  62.  
  63.             $currentDirection = ($currentDirection - $instruction.moves) % 360
  64.  
  65.             if ($currentDirection -lt 0) {
  66.                
  67.                 $currentDirection = $currentDirection + 360
  68.  
  69.             }
  70.        
  71.         }
  72.  
  73.         "R" {
  74.            
  75.             "Now turning Right by $($instruction.moves)"
  76.            
  77.             $currentDirection = ($currentDirection + $instruction.moves) % 360
  78.  
  79.             if ($currentDirection -lt 0) {
  80.                
  81.                 $currentDirection = $currentDirection + 360
  82.  
  83.             }
  84.        
  85.         }
  86.        
  87.         "F" {
  88.            
  89.             "Now moving Forward by $($instruction.moves)"
  90.  
  91.             switch ($currentDirection) {
  92.  
  93.                 0 { $currentPosition.NorthSouth = $currentPosition.NorthSouth + $instruction.moves }
  94.                 90 { $currentPosition.EastWest = $currentPosition.EastWest + $instruction.moves }
  95.                 180 { $currentPosition.NorthSouth = $currentPosition.NorthSouth - $instruction.moves }
  96.                 270 { $currentPosition.EastWest = $currentPosition.EastWest - $instruction.moves }
  97.  
  98.             }
  99.         }
  100.        
  101.     }
  102.  
  103.     "..."
  104. }
  105.  
  106. "Final position is"
  107. $currentPosition
  108.  
  109. "Puzzle solution is"
  110.  
  111. [Math]::Abs($currentPosition.NorthSouth) + [Math]::Abs($currentPosition.EastWest)
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement