Acquira

Advent of Code 2020 Day 1 Part 2

Dec 2nd, 2021 (edited)
536
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #https://adventofcode.com/2020/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. $list = Invoke-WebRequest -UseBasicParsing -Uri "https://adventofcode.com/2020/day/1/input"
  8. -WebSession $session `
  9.  
  10. #Transform the puzzle input into an array of int and sort it
  11. [int[]]$list = $list.Content -split("\n") | ? {$_}
  12. $list = $list | sort
  13.  
  14. #Using the two pointers technique to loop over the array
  15. for ($index = 0; $index -lt $list.Count -2 ; $index++){
  16.     $i = $index + 1
  17.     $j = $list.Count -1
  18.     while ($i -lt $j){
  19.         if($list[$index]+$list[$i]+$list[$j] -eq 2020){
  20.             $answer = $list[$index]*$list[$i]*$list[$j]
  21.             return $answer
  22.         }
  23.         elseif ($list[$index]+$list[$i]+$list[$j] -lt 2020){
  24.             $i++
  25.         }
  26.         else{
  27.             $j--
  28.         }
  29.     }
  30. }
RAW Paste Data