crlogic

Day8

Dec 8th, 2022
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. # Part 1
  2. [string[]]$grid = $puzzleInput -split '\n' | Select -SkipLast 1
  3. [int]$visible = ($grid.Length * 2) + (($grid.count - 2) * 2)
  4. $yMidCount = $grid.count - 2
  5. $xMidLen = $grid.Length - 2
  6. $yCount = $grid.count -1
  7. $xLen = $grid.Length -1
  8. for ($y=1; $y -le $yMidCount; $y++) {
  9. for ($x=1; $x -le $xMidLen; $x++) {
  10. $isVisible = $false
  11. switch ($grid[$y][$x]) {
  12. {($grid[$($y-1)..0] | foreach {$_[$x] -ge $grid[$y][$x]}) -notcontains $true} {$isVisible = $true;break}
  13. {($grid[$($y+1)..$yCount] | foreach {$_[$x] -ge $grid[$y][$x]}) -notcontains $true} {$isVisible = $true;break}
  14. {($grid[$y][$($x-1)..0] | foreach {$_ -ge $grid[$y][$x]}) -notcontains $true} {$isVisible = $true;break}
  15. {($grid[$y][$($x+1)..$xLen] | foreach {$_ -ge $grid[$y][$x]}) -notcontains $true} {$isVisible = $true;break}
  16. }
  17. if ($isVisible -eq $true) {$visible++}
  18. }
  19. }
  20. $visible
  21.  
  22. # Part 2
  23. $scenicScore = @{}
  24. for ($y=1; $y -le $yMidCount; $y++) {
  25. for ($x=1; $x -le $xMidLen; $x++) {
  26. $upScore = 0
  27. $downScore = 0
  28. $leftScore = 0
  29. $rightScore = 0
  30.  
  31. # Check up
  32. $up = $y
  33. do {$upScore++;$up--}
  34. until (($grid[$up][$x] -ge $grid[$y][$x]) -or ($up -eq 0))
  35.  
  36. # Check down
  37. $down = $y
  38. do {$downScore++;$down++}
  39. until (($grid[$down][$x] -ge $grid[$y][$x]) -or ($down -eq $yCount))
  40.  
  41. # Check left
  42. $left = $x
  43. do {$leftScore++;$left--}
  44. until (($grid[$y][$left] -ge $grid[$y][$x]) -or ($left -eq 0))
  45.  
  46. # Check right
  47. $right = $x
  48. do {$rightScore++;$right++}
  49. until (($grid[$y][$right] -ge $grid[$y][$x]) -or ($right -eq $xLen))
  50. $scenicScore["$y,$x"] = $upScore * $downScore * $leftScore * $rightScore
  51. }
  52. }
  53. $scenicScore.Values | Sort | Select -Last 1
Advertisement
Add Comment
Please, Sign In to add comment