Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #https://adventofcode.com/2020/day/1
- # WebRequest to gather your puzzle input
- $cookie_id = "53616xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
- $session.Cookies.Add((New-Object System.Net.Cookie("session", $cookie_id, "/", ".adventofcode.com")))
- $list = Invoke-WebRequest -UseBasicParsing -Uri "https://adventofcode.com/2020/day/1/input"
- -WebSession $session `
- #Transform the puzzle input into an array of int and sort it
- [int[]]$list = $list.Content -split("\n") | ? {$_}
- $list = $list | sort
- #Using the two pointers technique to loop over the array
- for ($index = 0; $index -lt $list.Count -2 ; $index++){
- $i = $index + 1
- $j = $list.Count -1
- while ($i -lt $j){
- if($list[$index]+$list[$i]+$list[$j] -eq 2020){
- $answer = $list[$index]*$list[$i]*$list[$j]
- return $answer
- }
- elseif ($list[$index]+$list[$i]+$list[$j] -lt 2020){
- $i++
- }
- else{
- $j--
- }
- }
- }
Add Comment
Please, Sign In to add comment