Acquira

Advent of Code 2021 Day 1 Part 2

Dec 2nd, 2021 (edited)
606
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #https://adventofcode.com/2021/day/1
  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. $measurements = Invoke-WebRequest -UseBasicParsing -Uri "https://adventofcode.com/2021/day/1/input"
  8. -WebSession $session `
  9.  
  10. # Format the input content as an array by splitting it line by line and removing empty lines
  11. $measurements = $measurements.Content -split("\n") | ? {$_}
  12.  
  13. $increase_count = 0
  14.  
  15. # Gather the elements 3 by 3 and do the sum of the values
  16. for ($i=1; $i -lt $measurements.Count; $i++){
  17.     if ((($measurements[($i-1)..($i+1)]|measure -sum).sum) -lt (($measurements[$i..($i+2)] | measure -Sum).sum)){
  18.         $increase_count++
  19.     }
  20. }
  21. $increase_count
  22.  
RAW Paste Data