Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #https://adventofcode.com/2020/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")))
- $map = Invoke-WebRequest -UseBasicParsing -Uri "https://adventofcode.com/2020/day/3/input"
- -WebSession $session
- $map = $map.Content -split ("\n")
- # Value of each required slopes (Part 2)
- # 1st value = horizontal movement
- # 2nd value = vertical movement
- $slopes = @(@(1,1),@(3,1),@(5,1),@(7,1),@(1,2))
- # Stores every results
- $results = @()
- $slopes | % {
- $result=$i=0
- $slope = $_
- 1..($map.Count -1) | % {
- # Modulo symbole is the same as the foreach shortcut : %
- # Move vertically depending on modulo x, x being $slope[1]:
- # Modulo = 0 -> Line by line
- # Modulo = 1 -> Jump 1 line between horizontal movements
- # Modulo = x -> Jump x lines between horizontal movements
- if($_ % $slope[1] -eq 0){
- #Move horizontally by x amounts, x being $slope[0]
- $i +=$slope[0]
- if ($i -gt 30){
- $i = $i % 31
- }
- if ($map[$_][$i] -eq '#'){
- $result += 1
- }
- # Visualisation of the map horizontal movements. Usefull to debug but slows down the solve by a lot
- #if ($i -eq 0){
- # Write-Host -NoNewline -ForegroundColor Red $map[$j][$i] ;Write-Host -NoNewline " " ;Write-Host $map[$j][($i+1)..31]
- #}
- #else{
- # Write-Host -NoNewline $map[$_][0..($i-1)] ;Write-Host -NoNewline " "; Write-Host -NoNewline -ForegroundColor Red $map[$_][$i] ;Write-Host -NoNewline " " ;Write-Host $map[$_][($i+1)..31]
- #}
- }
- # Visualisation of the map vertical movements using the modulo. Usefull to debug but slows down the solve by a lot.
- #else {
- # Write-Host $map[$_][0..31]
- #}
- }
- # Store the result as a string otherwise it would add up and mess up with the end result
- $results+=[string]$result
- }
- # Multiply together each of the different slopes results
- $result = 1
- 0..($results.Count -1) | % {
- $result=$result*[int]$results[$_]
- }
- # Final result
- $result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement