Acquira

Advent of Code 2020 Day 2 Both Parts

Dec 2nd, 2021 (edited)
658
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #https://adventofcode.com/2020/day/2
  2.  
  3. $cookie_id = "53616xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  4. $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
  5. $session.Cookies.Add((New-Object System.Net.Cookie("session", $cookie_id, "/", ".adventofcode.com")))
  6. $in = Invoke-WebRequest -UseBasicParsing -Uri "https://adventofcode.com/2020/day/2/input"
  7. -WebSession $session
  8. $in = $in.content -split ("\n")
  9.  
  10. $invalid=$invalid_2=0
  11.  
  12. #Some string manipulation to split appart both minimum and maximum, desired char and the password
  13. $in |? {$_} | % {
  14.     $policy     = $_.Split(":")
  15.     $password   = $policy.trim()[1]
  16.     $policy_char= $policy.trim()[0].Split(" ")[1]
  17.     $policy_min = $policy.trim()[0].Split(" ")[0].split("-")[0]
  18.     $policy_max = $policy.trim()[0].Split(" ")[0].split("-")[1]
  19.     $occurences = ([regex]::Matches($password, $policy_char)).count
  20.     if ($occurences -lt $policy_min -or $occurences -gt $policy_max){
  21.         $invalid +=1
  22.     }
  23.     #Exclusive or so both conditions cannot be met a the same time, as required
  24.     if (!(($password[[int]$policy_min-1] -like $policy_char) -xor ($password[[int]$policy_max-1] -like $policy_char))){
  25.         $invalid_2 += 1
  26.     }
  27. }
  28. $answers = @((1000-$invalid),(1000-$invalid_2) )
  29.  
  30.  
RAW Paste Data