Acquira

Advent of Code 2021 Day 2 Part 1

Dec 2nd, 2021 (edited)
716
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 = 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.             $depth += [int]$move[1]
  25.         }
  26.  
  27.         "up"{
  28.             $depth -= [int]$move[1]
  29.         }
  30.  
  31.         "forward"{
  32.             $horizontal+= [int]$move[1]
  33.         }
  34.     }
  35. }
  36.  
  37. $final_result = $depth*$horizontal
  38.  
RAW Paste Data