Advertisement
Acquira

Advent of Code 2020 Day 3 Both Parts

Dec 2nd, 2021 (edited)
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #https://adventofcode.com/2020/day/3
  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. $map = Invoke-WebRequest -UseBasicParsing -Uri "https://adventofcode.com/2020/day/3/input"
  8. -WebSession $session
  9.  
  10. $map = $map.Content -split ("\n")
  11.  
  12. # Value of each required slopes (Part 2)
  13. # 1st value = horizontal movement
  14. # 2nd value = vertical movement
  15. $slopes = @(@(1,1),@(3,1),@(5,1),@(7,1),@(1,2))
  16.  
  17. # Stores every results
  18. $results = @()
  19.  
  20.  
  21. $slopes | % {
  22.     $result=$i=0
  23.     $slope = $_
  24.     1..($map.Count -1) | % {
  25.         # Modulo symbole is the same as the foreach shortcut : %
  26.         # Move vertically depending on modulo x, x being $slope[1]:
  27.         # Modulo = 0  -> Line by line
  28.         # Modulo = 1  -> Jump 1 line between horizontal movements
  29.         # Modulo = x  -> Jump x lines between horizontal movements
  30.  
  31.         if($_ % $slope[1] -eq 0){  
  32.            
  33.             #Move horizontally by x amounts, x being $slope[0]
  34.             $i +=$slope[0]
  35.             if ($i -gt 30){
  36.                 $i = $i % 31
  37.             }
  38.             if ($map[$_][$i] -eq '#'){
  39.                 $result += 1
  40.             }
  41.  
  42.             # Visualisation of the map horizontal movements. Usefull to debug but slows down the solve by a lot
  43.             #if ($i -eq 0){
  44.             #    Write-Host -NoNewline -ForegroundColor Red $map[$j][$i] ;Write-Host -NoNewline " "  ;Write-Host $map[$j][($i+1)..31]
  45.             #}
  46.             #else{
  47.             #    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]
  48.             #}
  49.         }
  50.         # Visualisation of the map vertical movements using the modulo. Usefull to debug but slows down the solve by a lot.
  51.         #else {
  52.         #    Write-Host $map[$_][0..31]
  53.         #}
  54.     }
  55.     # Store the result as a string otherwise it would add up and mess up with the end result
  56.     $results+=[string]$result
  57. }
  58.  
  59. # Multiply together each of the different slopes results
  60. $result = 1
  61. 0..($results.Count -1) | % {
  62.     $result=$result*[int]$results[$_]
  63. }
  64.  
  65. # Final result
  66. $result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement