Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #https://adventofcode.com/2021/day/3
- # 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")))
- $binary = Invoke-WebRequest -UseBasicParsing -Uri "https://adventofcode.com/2021/day/3/input" ` -WebSession $session
- $binary = $binary.Content
- # Format the input content as an array by splitting it line by line
- $binary = $binary -split("\n")
- #Create a custom object to compare each bit "0" or "1"
- $counting_array = @()
- 0..($binary[0].Length -1) | % {
- $counting_array += [pscustomobject]@{
- "0"= 0
- "1"= 0
- }
- }
- #Parse over the puzzle input and count if bits are 0 or 1
- $binary | % {
- $bin = $_
- 0..($bin.length - 1)|% {
- if ($bin[$_] -eq "0"){
- $counting_array[$_].0 += 1
- }
- else{
- $counting_array[$_].1 += 1
- }
- }
- }
- #Store the binary as strings
- [string]$gamma = ""
- [string]$epsilon = ""
- #Compare for each bit if there are more 0 or 1 and append it to the binary strings
- $counting_array | % {
- if($_.0 -gt $_.1){
- $gamma+="0"
- $epsilon+="1"
- }
- else{
- $gamma+="1"
- $epsilon+="0"
- }
- }
- #Convert the strings to INT32 and multiply them to get the first part answer
- $part_1_answer = ([convert]::ToInt32($gamma,2))*([convert]::ToInt32($epsilon,2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement