Acquira

Advent of Code 2021 Day 2 Part 2

Dec 2nd, 2021 (edited)
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #https://adventofcode.com/2021/day/2
  2.  
  3. # WebRequest to gather your puzzle input
  4. $cookie_id = "53616xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  5. $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
  6. $session.Cookies.Add((New-Object System.Net.Cookie("session", $cookie_id, "/", ".adventofcode.com")))
  7. $moves = Invoke-WebRequest -UseBasicParsing -Uri "https://adventofcode.com/2021/day/2/input" ` -WebSession $session
  8. $moves = $moves.Content
  9.  
  10. [int]$horizontal = [int]$depth = [int]$aim =  0
  11.  
  12. # Format the input content as an array by splitting it line by line
  13. $moves = $moves -split("\n")
  14.  
  15. $moves  | % {
  16.  
  17.     # Split each row in 2 parts : command and value
  18.     $move = $_.split(" ")
  19.  
  20.     # Depending on the command, increase or decrease required values
  21.     switch($move[0]){
  22.  
  23.         "down"{
  24.             $aim += [int]$move[1]
  25.         }
  26.         "up"{
  27.             $aim -= [int]$move[1]
  28.         }
  29.         "forward"{
  30.             $horizontal += [int]$move[1]
  31.             if($aim -ne 0){
  32.                 $depth +=  [int]$move[1]*[int]$aim
  33.             }
  34.         }
  35.     }
  36. }
  37. $final_result = $depth*$horizontal
Add Comment
Please, Sign In to add comment